go-webpage

module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2025 License: MIT

README ยถ

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! ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰

Directories ยถ

Path Synopsis
Package firefoxopen: Firefox automation with webpage output Provides command-line automation to open URLs in Firefox Supports multiple window and tab opening modes with flexible URL handling Enables programmatic web content presentation via Firefox
Package firefoxopen: Firefox automation with webpage output Provides command-line automation to open URLs in Firefox Supports multiple window and tab opening modes with flexible URL handling Enables programmatic web content presentation via Firefox
Package gintestpage: HTTP test hosting with HTML page serving and development Provides lightweight HTTP hosting using Gin framework when testing pages Enables dynamic page registration and serving with memory-based storage Supports rapid prototyping and testing of HTML content using URL generation
Package gintestpage: HTTP test hosting with HTML page serving and development Provides lightweight HTTP hosting using Gin framework when testing pages Enables dynamic page registration and serving with memory-based storage Supports rapid prototyping and testing of HTML content using URL generation
internal
demos/demo1x command
demos/demo2x command
demos/demo3x command
demos/demo4x command
utils
Package utils: HTML page generation tools with XSS protection Provides basic HTML page construction using automatic content escaping Creates complete HTML documents with proper structure and XSS protection Supports customizable page titles and content using safe HTML rendering
Package utils: HTML page generation tools with XSS protection Provides basic HTML page construction using automatic content escaping Creates complete HTML documents with proper structure and XSS protection Supports customizable page titles and content using safe HTML rendering
Package slice2table: Convert Go struct slice to HTML table with customizable rendering options Features automatic field extraction, tag-based column naming, and flexible HTML table generation Supports custom tag names and intelligent field filtering to create clean table presentation Provides seamless struct-to-HTML transformation with built-in HTML escaping
Package slice2table: Convert Go struct slice to HTML table with customizable rendering options Features automatic field extraction, tag-based column naming, and flexible HTML table generation Supports custom tag names and intelligent field filtering to create clean table presentation Provides seamless struct-to-HTML transformation with built-in HTML escaping
struct2table
Package struct2table: Single struct to HTML table conversion tool Provides convenient functions when converting individual struct instances to HTML tables Leverages slice2table package for core functionality with single-item slice wrapping Simplifies table generation workflow for single object display scenarios
Package struct2table: Single struct to HTML table conversion tool Provides convenient functions when converting individual struct instances to HTML tables Leverages slice2table package for core functionality with single-item slice wrapping Simplifies table generation workflow for single object display scenarios
Package w3mopenpage: W3m console browser automation for webpage output Provides command-line webpage rendering using w3m text browser Enables HTML content display in console environment with text-based output Supports both URL opening and direct HTML content processing via w3m
Package w3mopenpage: W3m console browser automation for webpage output Provides command-line webpage rendering using w3m text browser Enables HTML content display in console environment with text-based output Supports both URL opening and direct HTML content processing via w3m

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL