How To Write Go Code
- tags
- Go
- source
- How To Write Go Code
Code Organization
Module
- collection of packages
- usually one directory contains one module
- module has
go.mod
file containing description of the it
Package
- collection of source code files
- source code files in one package share the same scope
functions, variables, constants
Init, run, test, install
Init
go mod init <module prefix>
declare a module and create a go.mod
automatically
Run
go run <main package file>
Test
go test
- file name convention:
xxx_test.go
- test function name convention:
func TestXXX(t *testing.T)
Install
go install <path>
- if currently in module root dir,
<path>
can be omitted - module will be installed to:
- if
GOBIN
is set, binary goes to that directory - if
GOPATH
is set, binary goes to the first directory’s bin
subdir