Skip to content

go-xlan/go-webpage

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

go-webpage

Convert Go struct slice to HTML table with browser automation.


CHINESE README

中文说明

Main Features

🎯 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

Installation

go get github.com/go-xlan/go-webpage

Quick Start

Basic Table Generation

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)
}

Browser Output

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

Console Output

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

Advanced Usage

Custom Tag Names

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)

Single Struct Table

import "github.com/go-xlan/go-webpage/slice2table/struct2table"

user := &User{Name: "Alice", Age: 25}
table := struct2table.NewTable(user)

API Reference

slice2table Package

  • NewTable[T any](objects []*T) string - Convert struct slice to HTML table
  • GenTable[T any](objects []*T, options *Options) string - Generate table with custom options
  • NewOptions() *Options - Create default options
  • (opts *Options) WithTagName(tagName string) *Options - Set custom tag name

firefoxopen Package

  • OpenInNewWindows(command *osexec.OsCommand, urls ...string) - Open URLs in new Firefox windows
  • OpenInNewTabs(command *osexec.OsCommand, urls ...string) - Open URLs in new Firefox tabs
  • NewFirefoxDraw() *FirefoxShow - Create Firefox output handler

w3mopenpage Package

  • NewW3mDrawPage() *W3mShowPage - Create w3m output handler
  • Show(page string) - Show single page in console
  • ShowPages(pages ...string) - Show multiple pages

gintestpage Package

  • NewService() *Service - Create HTTP test server
  • SetPage(path string, page []byte) string - Set page and get URL
  • GetLink(path string) string - Get URL of page path

utils Package

  • NewPage(subject string, content string) string - Create complete HTML page with escaping

Examples

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

📄 License

MIT License. See LICENSE.


🤝 Contributing

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

🔧 Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

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! 🎉🎉🎉

About

convert golang struct-slice to web table

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •