Example of the talk https://t3easy.github.io/talks/go-fundamentals/
-
go mod init github.com/t3easy/hello-go
- Add main.go and run it
go run main.go
- Add test and run it
go test ./...
- Install cobra-cli
go install github.com/spf13/cobra-cli@latest
- Rename main.go, to temp.go
- Init module with cobra-cli
~/go/bin/cobra-cli init
- Add functionality of previous
main.go
tocmd/root.go
,
movemain_test.go
tocmd/root_test.go
- Test and run
go test ./... go run main.go
- Add
--name
parameter that defaults toGo
- Module housekeeping
go mod tidy
- Add
serve
command and run it~/go/bin/cobra-cli add serve go run main.go serve
- Move
getMessage
function togithub.com/t3easy/hello-go/internal
with capitalG
! As we will need it in theserve
cmd, too. Run tests to check if nothing is broken.go test ./... go run main.go --name FooBar
- Add http server for the "API"
Open http://127.0.0.1:4242/
go run main.go serve
Open http://127.0.0.1:4242/FooBar - Add
--port
flag to serve command - Build binary
go build -o bin