Basic Examples
Simple, practical examples to get you started with goTableView.
Example 1: Hello World Table
The simplest possible table:
go
package main
import "github.com/mansoldof/goTableView"
func main() {
gotableview.New("Hello World").
Columns("Message").
Row("Hello from goTableView!").
Show()
}Output:
Screenshot: Simple window with one column "Message" and one row "Hello from goTableView!"
Example 2: Contact List
Display a simple contact list:
go
package main
import "github.com/mansoldof/goTableView"
func main() {
gotableview.New("My Contacts").
Columns("Name", "Phone", "Email").
Row("Alice Johnson", "(555) 123-4567", "alice@example.com").
Row("Bob Smith", "(555) 234-5678", "bob@example.com").
Row("Carol White", "(555) 345-6789", "carol@example.com").
Show()
} Screenshot: Contact list table with 3 contacts showing Name, Phone, and Email columns
Example 3: Shopping Cart
Display items in a shopping cart:
go
package main
import "github.com/mansoldof/goTableView"
func main() {
cart := [][]string{
{"Laptop", "1", "$999.99", "$999.99"},
{"Mouse", "2", "$24.99", "$49.98"},
{"Keyboard", "1", "$74.99", "$74.99"},
{"Monitor", "2", "$299.99", "$599.98"},
}
gotableview.New("Shopping Cart").
Columns("Product", "Quantity", "Unit Price", "Total").
Rows(cart).
Show()
} Screenshot: Shopping cart with 4 items showing Product, Quantity, Unit Price, and Total
Example 4: Grade Book
Teacher's grade book:
go
package main
import "github.com/mansoldof/goTableView"
func main() {
grades := [][]string{
{"Alice", "95", "88", "92", "A"},
{"Bob", "82", "79", "85", "B"},
{"Carol", "91", "94", "89", "A"},
{"David", "78", "82", "75", "B"},
{"Eve", "88", "91", "93", "A"},
}
gotableview.New("Student Grades").
Columns("Student", "Test 1", "Test 2", "Test 3", "Grade").
ColumnWidths(150, 80, 80, 80, 80).
Rows(grades).
Show()
} Screenshot: Grade book showing 5 students with test scores and final grades
Example 5: Task List
Simple to-do list manager:
go
package main
import "github.com/mansoldof/goTableView"
func main() {
tasks := [][]string{
{"1", "Write documentation", "High", "In Progress"},
{"2", "Fix bugs", "Medium", "Not Started"},
{"3", "Add tests", "High", "Not Started"},
{"4", "Update README", "Low", "Completed"},
{"5", "Release v1.0", "High", "Not Started"},
}
gotableview.New("Task List").
Columns("ID", "Task", "Priority", "Status").
Rows(tasks).
Show()
} Screenshot: Task list with 5 tasks showing ID, Task, Priority, and Status
Example 6: Server Status
Monitor server status:
go
package main
import (
"fmt"
"time"
"github.com/mansoldof/goTableView"
)
func main() {
servers := [][]string{
{"web-01", "Running", "99.9%", "0.45"},
{"web-02", "Running", "99.8%", "0.52"},
{"db-01", "Running", "100%", "0.23"},
{"cache-01", "Down", "98.5%", "0.00"},
{"api-01", "Running", "99.9%", "0.67"},
}
title := fmt.Sprintf("Server Status - %s", time.Now().Format("15:04:05"))
gotableview.New(title).
Columns("Server", "Status", "Uptime", "Load").
Rows(servers).
Show()
} Screenshot: Server status table showing 5 servers with status, uptime, and load metrics
Example 7: Product Inventory
Store inventory management:
go
package main
import "github.com/mansoldof/goTableView"
type Product struct {
SKU string
Name string
Price float64
Stock int
}
func main() {
products := []Product{
{"LAP-001", "Gaming Laptop", 1299.99, 5},
{"MOU-001", "Wireless Mouse", 29.99, 45},
{"KEY-001", "Mechanical Keyboard", 89.99, 23},
{"MON-001", "27\" Monitor", 349.99, 12},
{"HDP-001", "USB-C Hub", 49.99, 67},
}
gotableview.FromStructs("Product Inventory", products).Show()
} Screenshot: Product inventory with automatic columns from struct (SKU, Name, Price, Stock)
Example 8: Weather Data
Display weather forecast:
go
package main
import "github.com/mansoldof/goTableView"
func main() {
forecast := [][]string{
{"Monday", "Sunny", "75°F", "45°F", "10%"},
{"Tuesday", "Cloudy", "68°F", "50°F", "30%"},
{"Wednesday", "Rainy", "62°F", "48°F", "80%"},
{"Thursday", "Partly Cloudy", "70°F", "52°F", "20%"},
{"Friday", "Sunny", "78°F", "55°F", "5%"},
}
gotableview.New("5-Day Weather Forecast").
Columns("Day", "Condition", "High", "Low", "Rain").
Rows(forecast).
Show()
} Screenshot: Weather forecast table showing 5 days with conditions, temperatures, and rain probability
Example 9: File Browser
Browse directory contents:
go
package main
import (
"fmt"
"os"
"github.com/mansoldof/goTableView"
)
func main() {
entries, _ := os.ReadDir(".")
table := gotableview.New("Directory Browser")
table.Columns("Name", "Type", "Size")
for _, entry := range entries {
fileType := "File"
size := ""
if entry.IsDir() {
fileType = "Directory"
size = "-"
} else {
info, _ := entry.Info()
size = fmt.Sprintf("%d bytes", info.Size())
}
table.Row(entry.Name(), fileType, size)
}
table.Show()
} Screenshot: Directory browser showing files and folders with names, types, and sizes
Example 10: Simple Calculator Results
Display calculation results:
go
package main
import (
"fmt"
"github.com/mansoldof/goTableView"
)
func main() {
results := [][]string{}
for i := 1; i <= 10; i++ {
results = append(results, []string{
fmt.Sprintf("%d", i),
fmt.Sprintf("%d", i*i),
fmt.Sprintf("%d", i*i*i),
fmt.Sprintf("%.2f", float64(i)*1.5),
})
}
gotableview.New("Calculation Results").
Columns("Number", "Square", "Cube", "× 1.5").
Rows(results).
Show()
} Screenshot: Mathematical calculations table showing numbers 1-10 with their squares, cubes, and multiplication results
Example 11: Custom Column Widths
Control column sizing:
go
package main
import "github.com/mansoldof/goTableView"
func main() {
gotableview.New("Custom Widths Demo").
Columns("Short", "Medium Column", "Very Long Description Column", "ID").
ColumnWidths(60, 150, 400, 50).
Row("A", "Some text", "This is a very long description that needs more space to display properly", "1").
Row("B", "More text", "Another lengthy description with lots of important information", "2").
Show()
} Screenshot: Table with varying column widths - narrow ID, medium text, and wide description column
Example 12: Environment Variables
Display system environment variables:
go
package main
import (
"os"
"strings"
"github.com/mansoldof/goTableView"
)
func main() {
envVars := os.Environ()
table := gotableview.New("Environment Variables")
table.Columns("Variable", "Value")
for _, env := range envVars {
parts := strings.SplitN(env, "=", 2)
if len(parts) == 2 {
table.Row(parts[0], parts[1])
}
}
table.Show()
} Screenshot: Environment variables table showing variable names and their values
Example 13: Book Library
Manage a book collection:
go
package main
import "github.com/mansoldof/goTableView"
type Book struct {
Title string `gotableview:"Book Title"`
Author string `gotableview:"Author"`
Year int `gotableview:"Year"`
Pages int `gotableview:"Pages"`
Rating float64 `gotableview:"Rating"`
Available bool `gotableview:"Available"`
}
func main() {
library := []Book{
{"The Go Programming Language", "Donovan & Kernighan", 2015, 380, 4.8, true},
{"Clean Code", "Robert Martin", 2008, 464, 4.7, true},
{"The Pragmatic Programmer", "Hunt & Thomas", 1999, 352, 4.9, false},
{"Design Patterns", "Gang of Four", 1994, 416, 4.6, true},
}
gotableview.FromStructs("Book Library", library).Show()
} Screenshot: Book library with 4 books showing structured data with custom column headers
Example 14: Quick Statistics
Display data statistics:
go
package main
import (
"fmt"
"github.com/mansoldof/goTableView"
)
func main() {
data := []float64{10.5, 23.2, 15.7, 45.3, 12.8, 33.1}
sum := 0.0
min := data[0]
max := data[0]
for _, v := range data {
sum += v
if v < min {
min = v
}
if v > max {
max = v
}
}
avg := sum / float64(len(data))
stats := [][]string{
{"Count", fmt.Sprintf("%d", len(data))},
{"Sum", fmt.Sprintf("%.2f", sum)},
{"Average", fmt.Sprintf("%.2f", avg)},
{"Minimum", fmt.Sprintf("%.2f", min)},
{"Maximum", fmt.Sprintf("%.2f", max)},
}
gotableview.New("Data Statistics").
Columns("Metric", "Value").
Rows(stats).
Show()
} Screenshot: Statistics table showing Count, Sum, Average, Minimum, and Maximum values
Example 15: Non-Blocking Windows
Display multiple windows simultaneously or continue execution while window is open:
go
package main
import (
"fmt"
"time"
"github.com/mansoldof/goTableView"
)
func main() {
// Example 1: Show window and continue execution
gotableview.New("Live Statistics").
Columns("Metric", "Value").
Row("Status", "Running").
Row("Uptime", "5 minutes").
Show(false) // Non-blocking - returns immediately
fmt.Println("Window is open, but code continues...")
// Example 2: Multiple windows at once
salesData := [][]string{
{"Q1", "$125,000"},
{"Q2", "$148,000"},
{"Q3", "$152,000"},
}
expenseData := [][]string{
{"Q1", "$85,000"},
{"Q2", "$92,000"},
{"Q3", "$88,000"},
}
// Open first window (non-blocking)
gotableview.New("Sales Report").
Columns("Quarter", "Revenue").
Rows(salesData).
Show(false)
// Open second window (non-blocking)
gotableview.New("Expense Report").
Columns("Quarter", "Expenses").
Rows(expenseData).
Show(false)
fmt.Println("Both windows are now open!")
// Simulate ongoing work
for i := 1; i <= 5; i++ {
fmt.Printf("Processing step %d...\n", i)
time.Sleep(1 * time.Second)
}
// Show final results and wait (blocking)
gotableview.New("Final Summary").
Columns("Report", "Status").
Row("Sales", "Complete").
Row("Expenses", "Complete").
Row("Analysis", "Done").
Show() // Default blocking behavior - waits here
fmt.Println("All windows closed, program ending")
}Key Points:
Show()orShow(true)- Blocking (default): Code waits until window is closedShow(false)- Non-blocking: Window opens and code continues immediately- Perfect for displaying live data or multiple comparison windows
- All windows remain interactive and responsive
Screenshot: Multiple windows open simultaneously showing Sales Report, Expense Report, and continuing execution in console
Running the Examples
All examples can be run directly:
- Create a new Go file (e.g.,
example.go) - Copy the example code
- Run with
go run example.go
bash
# Create and run an example
echo 'package main...' > example.go
go run example.goWhat's Next?
- Explore Advanced Examples for complex scenarios
- Learn about plyGO Integration
- Check the API Reference for more details