Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// Package layout for a small web service: // // myapp/ // go.mod // main.go (package main) // cmd/ // server/ // main.go (package main — the binary entrypoint) // internal/ // config/ // config.go (package config — unexported to outside module) // handler/ // user.go (package handler) // pkg/ // validate/ // validate.go (package validate — public reusable library) // internal/config/config.go package config import "os" // Config holds application configuration. type Config struct { Port string DSN string LogLevel string } // Load reads configuration from environment variables. func Load() Config { port := os.Getenv("PORT") if port == "" { port = "8080" } return Config{ Port: port, DSN: os.Getenv("DATABASE_URL"), LogLevel: os.Getenv("LOG_LEVEL"), } } // pkg/validate/validate.go package validate import ( "errors" "strings" ) // Email returns an error if s is not a valid email address. func Email(s string) error { if !strings.Contains(s, "@") { return errors.New("validate: invalid email address") } return nil }
Result
Open