Golang Learning Point

Golang Learning Point

Reasons Golang Was Created

  1. Simplicity
  2. Fast compilation
  3. Compiled language
  4. Strongly typed
  5. Built-in concurrency support
  6. Automatic garbage collection

Applicable Scenarios

  1. Web Development
  2. Command-line Interfaces(CLIs)
  3. Cloud & NetWork Services

Golang Compatibility

GO 1.X new versions are guaranteed to be backward compatible, except for the following four cases

  1. Security errors
  2. Specification errors
  3. Unintended behaviors
  4. Bugs

Common Command-Line Commands: Running Programs

  1. go run . (run the program)
  2. go build .(build the project)

go build -v -o dist/main . (show verbose output during build and specify output path)

  1. go clean (clean the project — delete files generated during the build process)
  • go clean -i (also delete installed programs)
  • go clean -i -n (the -n flag previews files that would be deleted)
  • go clean -cache (clean build cache files)
  • go clean -cache -n (preview cleaning of build cache files)
  • go clean -modcache -x (clean module cache and display delete commands)
  • go clean -i -cache -modcache -x

Common Command-Line Commands: Running Tests

  1. File naming rule: *_test.go
  2. Running the program: not applicable (using go run . will automatically ignore *_test.go files)
  3. Building the project: no need to build (using go build will automatically ignore *_test.go files)
  • You can use go test -c to compile the test binary (but it will not run the tests)
  • Run tests: go test .
  • Clean cache: go clean -testcache
  • Benchmark testing: go test -bench=. -v

Other Common Command-Line Commands

  1. go version (display the Go runtime version)
  2. go env (display Go-related environment variables)
  3. Other commands well-integrated by development tools
  • go get download packages
  • go fmt format source code
  • go vet check source code quality
  • go fix automatically fix problematic source code
  • go doc retrieve API documentation from source code

Golang Cross-Platform Support

Reserved Keywords

25Keyword

Keyword reference

Automatic Semicolon Insertion Rules

  1. Identifiers
  2. Values
  • integer, floating-point, imaginary, rune, string
  • true and false are not values
  1. The following four keywords
  • break
  • continue
  • return
  • fallthrough
  1. The following five operators and punctuation marks
  • )
  • }
  • ]
  • ++
  • --

Naming Conventions

Naming Convertion

Variable Declaration

var score int = 95

  • var is a keyword
  • score is the variable name
  • int is the type
  • 95 is the initial value

":=" declares a variable and assigns a value A “*” prefix indicates a pointer

There are two ways to declare variables

1
2
3
4
5
 var i = "G is" + " for Go "
 var j = 'v'
 var k1, k2 = true, !k1
 const l = 111*10000 + 9
 const m1 = math.pi / 3.141592
1
2
3
4
5
6
7
8
var (
    name string = "Earth"
    desc string = "Planet"
    radius int32 = 6378
    mass float64 = 5.972E+24
    active bool = true
    satellites []string
)

Zero Value

Zero value table

Type Inference

var name = "will" Shorthand syntax name := "will"

Constant Declaration (Basic Types Only)

  • Numeric: byte, int, int8, int16, int32, int64, rune, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128

  • Boolean: bool

  • String: string

Untyped Constants

untyped constant

Auto-Incrementing Constants with iota

1
2
3
4
5
6
7
const (
    Zero = iota
    One
    Two
    Three
    Four
)

Is equivalent to

1
2
3
4
5
6
7
const (
    Zero = 0
    One = 1
    Two = 2
    Three = 3
    Four = 4
)

References