Convert Go struct slice to HTML table with Firefox/w3m output.
🎯 Struct to HTML Table: Convert Go struct slices to formatted HTML tables with custom tag support ⚡ HTML Output: Open generated HTML in Firefox; display via w3m console 🔄 HTTP Test Host: Built-in HTTP test service supporting rapid webpage development and testing 🌍 Flexible Options: Customizable table headers, tag names, and rendering configurations 📋 XSS Protection: HTML escaping built-in to prevent XSS vulnerabilities
go get github.com/go-xlan/go-webpagepackage main
import (
"fmt"
"github.com/go-xlan/go-webpage/slice2table"
)
type Account struct {
Name string `table:"姓名"`
Age int `table:"年龄"`
}
func main() {
accounts := []*Account{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
}
page := slice2table.NewTable(accounts)
fmt.Println(page)
}package main
import (
"fmt"
"time"
"github.com/brianvoe/gofakeit/v7"
"github.com/go-xlan/go-webpage/firefoxview"
"github.com/go-xlan/go-webpage/slice2table"
"github.com/yyle88/must"
)
type AdminInfo struct {
Name string
From string
}
type GuestInfo struct {
Name string
From string
}
func main() {
admins := []*AdminInfo{
newAdminInfo(),
newAdminInfo(),
newAdminInfo(),
}
page1 := slice2table.NewTable(admins)
guests := []*GuestInfo{
newGuestInfo(),
newGuestInfo(),
newGuestInfo(),
}
page2 := slice2table.NewTable(guests)
pane := firefoxview.NewPane()
defer pane.Close(time.Minute)
pane.ShowInNewTabs(page1, page2)
fmt.Println("done")
}
func newAdminInfo() *AdminInfo {
admin := &AdminInfo{}
must.Done(gofakeit.Struct(admin))
return admin
}
func newGuestInfo() *GuestInfo {
guest := &GuestInfo{}
must.Done(gofakeit.Struct(guest))
return guest
}⬆️ Source: Source
package main
import (
"fmt"
"time"
"github.com/brianvoe/gofakeit/v7"
"github.com/go-xlan/go-webpage/slice2table"
"github.com/go-xlan/go-webpage/w3mview"
"github.com/yyle88/must"
"github.com/yyle88/osexec"
)
type Account struct {
ID string // 账户 ID
Username string // 用户名
Mailbox string // 邮箱
Role string // 角色(如 admin, guest, student)
IsActive bool // 是否激活
CreatedAt time.Time // 创建时间
}
type Invoice struct {
ID string // 账单 ID
AccountID string // 账户 ID
Amount float64 // 金额
Status string // 状态(如 pending, shipped)
IssuedAt time.Time // 签发时间
}
func main() {
accounts := []*Account{
newAccount(),
newAccount(),
newAccount(),
}
page1 := slice2table.NewTable(accounts)
invoices := []*Invoice{
newInvoice(),
newInvoice(),
newInvoice(),
}
page2 := slice2table.NewTable(invoices)
execConfig := osexec.NewOsCommand().WithDebug()
w3mview.Show(execConfig, page1)
w3mview.Show(execConfig, page2)
fmt.Println("done")
}
func newAccount() *Account {
account := &Account{}
must.Done(gofakeit.Struct(account))
return account
}
func newInvoice() *Invoice {
invoice := &Invoice{}
must.Done(gofakeit.Struct(invoice))
return invoice
}⬆️ Source: Source
type Student struct {
Name string `custom:"Student Name"`
Grade int `custom:"Grade Rank"`
}
students := []*Student{
{Name: "John", Grade: 95},
}
options := slice2table.NewOptions().WithTagName("custom")
table := slice2table.GenTable(students, options)import "github.com/go-xlan/go-webpage/slice2table/struct2table"
account := &Account{Name: "Alice", Age: 25}
table := struct2table.NewTable(account)NewTable[T any](objects []*T) string- Convert struct slice to HTML tableGenTable[T any](objects []*T, options *Options) string- Generate table with custom optionsNewOptions() *Options- Create default options(opts *Options) WithTagName(tagName string) *Options- Set custom tag name
NewTable[T any](object *T) string- Convert single struct to HTML tableGenTable[T any](object *T, options *slice2table.Options) string- Generate table with custom options
Open(execConfig *osexec.OsCommand, urls []string, mode string)- Open URLs in Firefox with modeOpenInNewWindows(execConfig *osexec.OsCommand, urls ...string)- Open URLs in new Firefox windowsOpenInNewTabs(execConfig *osexec.OsCommand, urls ...string)- Open URLs in new Firefox tabsNewPane() *Pane- Create Firefox output pane with default configNewPaneWith(pageSite *mocksite.PageSite, execConfig *osexec.OsCommand) *Pane- Create pane with custom config(p *Pane) Close(waitTime time.Duration)- Close pane when wait time elapses(p *Pane) ShowInNewWindows(pages ...string)- Show pages in new windows(p *Pane) ShowInNewTabs(pages ...string)- Show pages in new tabs(p *Pane) Show(pages []string, mode string)- Show pages with mode(p *Pane) Open(pages []string, mode string)- Alias of Show
Show(execConfig *osexec.OsCommand, page string)- Display HTML page via w3m consoleOpen(execConfig *osexec.OsCommand, link string)- Open URL via w3m consoleDumpPage(execConfig *osexec.OsCommand, page string) string- Process HTML and get text outputDumpLink(execConfig *osexec.OsCommand, link string) string- Fetch URL and return textNewPane() *Pane- Create w3m output pane with default configNewPaneWith(pageSite *mocksite.PageSite, execConfig *osexec.OsCommand) *Pane- Create pane with custom config(p *Pane) Close()- Close pane and release resources(p *Pane) Show(page string)- Show single page via pane(p *Pane) ShowPages(pages ...string)- Show multiple pages via pane
NewPageSite() *PageSite- Create HTTP test instance(s *PageSite) Close()- Close test instance and release resources(s *PageSite) SetPage(path string, page []byte) string- Set page and get access link(s *PageSite) GetLink(path string) string- Get URL of page path
Check out the examples path with more usage patterns:
- demo1x - Mock site service with Firefox automation
- demo2x - Multiple tables in Firefox tabs
- demo3x - Console output with w3m
- demo4x - W3m page output with multiple datasets
MIT License - see LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- 🐛 Mistake reports? Open an issue on GitHub with reproduction steps
- 💡 Fresh ideas? Create an issue to discuss
- 📖 Documentation confusing? Report it so we can enhance it
- 🚀 Need new features? Share the use cases to help us understand requirements
- ⚡ Performance issue? Help us optimize via reporting slow operations
- 🔧 Configuration problem? Ask questions about complex setups
- 📢 Track project progress? Watch the repo to get new releases and features
- 🌟 Success stories? Share how this package enhanced the workflow
- 💬 Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). - Navigate: Navigate to the cloned project (
cd repo-name) - Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and adhere to Go code style conventions - Documentation: Update documentation to support client-facing changes
- Stage: Stage changes (
git add .) - Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- ⭐ Give GitHub stars if this project helps you
- 🤝 Share with teammates and (golang) programming friends
- 📝 Write tech blogs about development tools and workflows - we provide content writing support
- 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! 🎉🎉🎉