It's time to configure our application. For configuration, we will use the flag package flag
import "flag"
func main() {
bindAddr := flag.String("bind_addr", ":8080", "Set bind address")
flag.Parse()
a := api.New(*bindAddr)
log.Fatal(a.Start())
}
In GO there are several rules of good taste:
- There should be tests in your package
- Your libraries should not write logs
- Error messages should be written in lowercase
- Your code must be formatted
To keep the code always formatted, in addition to saving triggers, you can run go fmt ./...
and it will format all files. For an example I will share with, you a simple Makefile
TARGET=codelab
all: fmt clean build
clean:
rm -rf $(TARGET)
depends:
go get -u -v
build:
go build -v -o $(TARGET) main.go
fmt:
go fmt ./...
test:
go test -v ./...
run:
go run main.go
You will format all the code by making “make” and rebuild the project, and make run will start the project for you. In the next step we will write tests for our API and learn about coverage