Configuring Go application with flags, pflags, environment variables and Viper

There are multiple ways to configure your Go application. I will describe a few most common one in this article with code samples. Flags Go supports command line flags with builtin package flag. package main import ( "flag" "log" "net/http" ) func main() { addrPtr := flag.String("addr", ":8000", "addr of http server") flag.Parse() log.Printf("Listening on %s", *addrPtr) log.Fatal(http.ListenAndServe(*addrPtr, nil)) } func main() { var addr string flag.StringVar(&addr, "addr", ":8000", "addr of http server") flag....

November 6, 2023 · 3 min

Serving Static Website With Go

Serving static website in Go I would like to describe here how to serve static content with Go. Go http package provides handy function http.FileServer to serve such content. FileServer also allows to browse files when index.html is missing in a catalog. Preparing files Let’s create such structure. $ tree . ├── assets │ ├── page.html │ └── styles │ └── main.css └── main.go File: assets/page.html <!doctype html> <html> <head> <meta charset="utf-8"> <title>Document</title> <link rel="stylesheet" href="/styles/main....

October 25, 2023 · 2 min