Convert Go struct slice to HTML table with browser automation.
🎯 Struct to HTML Table: Convert Go struct slices to formatted HTML tables with custom tag support ⚡ Browser Automation: Open generated HTML in Firefox; render in w3m console 🔄 Test Server: Built-in HTTP test server 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-webpage
package main
import (
"fmt"
"github.com/go-xlan/go-webpage/slice2table"
)
type User struct {
Name string `table:"姓名"`
Age int `table:"年龄"`
}
func main() {
users := []*User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
}
htmlTable := slice2table.NewTable(users)
fmt.Println(htmlTable)
}
package main
import (
"fmt"
"time"
"github.com/brianvoe/gofakeit/v7"
"github.com/go-xlan/go-webpage/firefoxopen"
"github.com/go-xlan/go-webpage/internal/utils"
"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 := utils.NewPage("admins", slice2table.NewTable(admins))
guests := []*GuestInfo{
newGuestInfo(),
newGuestInfo(),
newGuestInfo(),
}
page2 := utils.NewPage("guests", slice2table.NewTable(guests))
firefoxDraw := firefoxopen.NewFirefoxDraw()
defer firefoxDraw.Close(time.Minute)
firefoxDraw.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/internal/utils"
"github.com/go-xlan/go-webpage/slice2table"
"github.com/go-xlan/go-webpage/w3mopenpage"
"github.com/yyle88/must"
"github.com/yyle88/osexec"
)
type User struct {
ID string // 用户 ID
Username string // 用户名
Email string // 邮箱
Role string // 角色(如 admin, guest, student)
IsActive bool // 是否激活
CreatedAt time.Time // 创建时间
}
type Order struct {
ID string // 订单 ID
UserID string // 用户 ID
Total float64 // 总金额
Status string // 状态(如 pending, shipped)
OrderedAt time.Time // 下单时间
}
func main() {
users := []*User{
newUser(),
newUser(),
newUser(),
}
page1 := utils.NewPage("users", slice2table.NewTable(users))
orders := []*Order{
newOrder(),
newOrder(),
newOrder(),
}
page2 := utils.NewPage("orders", slice2table.NewTable(orders))
commandConfig := osexec.NewOsCommand().WithDebug()
w3mopenpage.Show(commandConfig, page1)
w3mopenpage.Show(commandConfig, page2)
fmt.Println("done")
}
func newUser() *User {
user := &User{}
must.Done(gofakeit.Struct(user))
return user
}
func newOrder() *Order {
order := &Order{}
must.Done(gofakeit.Struct(order))
return order
}
⬆️ Source: Source
type Student struct {
Name string `custom:"Student Name"`
Grade int `custom:"Grade Level"`
}
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"
user := &User{Name: "Alice", Age: 25}
table := struct2table.NewTable(user)
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
OpenInNewWindows(command *osexec.OsCommand, urls ...string)
- Open URLs in new Firefox windowsOpenInNewTabs(command *osexec.OsCommand, urls ...string)
- Open URLs in new Firefox tabsNewFirefoxDraw() *FirefoxShow
- Create Firefox output handler
NewW3mDrawPage() *W3mShowPage
- Create w3m output handlerShow(page string)
- Show single page in consoleShowPages(pages ...string)
- Show multiple pages
NewService() *Service
- Create HTTP test serverSetPage(path string, page []byte) string
- Set page and get URLGetLink(path string) string
- Get URL of page path
NewPage(subject string, content string) string
- Create complete HTML page with escaping
Check out the examples path with more usage patterns:
- demo1x - Gin test server 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:
- 🐛 Found a mistake? Open an issue on GitHub with reproduction steps
- 💡 Have a feature idea? Create an issue to discuss the suggestion
- 📖 Documentation confusing? Report it so we can improve
- 🚀 Need new features? Share the use cases to help us understand requirements
- ⚡ Performance issue? Help us optimize through reporting slow operations
- 🔧 Configuration problem? Ask questions about complex setups
- 📢 Follow project progress? Watch the repo to get new releases and features
- 🌟 Success stories? Share how this package improved 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 follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
- 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! 🎉🎉🎉