type
status
date
slug
summary
tags
category
icon
password
🔔
Prelude: No need for digging a big hole, just use it.

Golang in short


Go is

  1. A modern high-level programming language that resembles C
  1. Open-source, compiled, statically typed, and memory-safe

Go has

  1. Simple yet powerful package management (pulls directly from GitHub)
  1. Built-in concurrency primitives
  1. Garbage collection (GC)
  1. Lightning-fast compilation
  1. Minimal syntax
  1. Cross-platform compilation

Go doesn't have

  1. Classes and inheritance
  1. Function overloading
  1. Implicit type conversions

I use Go to

  1. Say goodbye to Java's verbose syntax
  1. Build web services and microservices
  1. Handle high-concurrency scenarios

Go is perfect for

  1. Web services and RESTful APIs
  1. Microservices architecture
  1. Network programming and distributed systems

Go might not be ideal for

  1. GUI desktop applications
  1. Kernel development
  1. Machine learning (Python has a stronger ecosystem)
 

Installing Go

  1. Using OS package managers
      • Linux: apt, yum, snap
      • macOS: brew
      • Windows: choco, scoop, etc.
  1. Download official precompiled binaries
      • Already compiled, ready to use
      • Set up path and links manually
  1. Verify with go version

Hello World

Types

Variables and Functions

Arrays, Slices, and Maps

Control Flow

Defer and Channels

Pointers

Format Verbs

Verb
Description
Example
Output
%v (value)
Default format
Printf("%v", people)
{zhangsan}
%+v
Adds field names for structs
Printf("%+v", people)
{Name:zhangsan}
%#v
Go syntax representation
Printf("%#v", people)
main.Human{Name:"zhangsan"}
%T (type)
Type in Go syntax
Printf("%T", people)
main.Human
%%
Percent sign
Printf("%%")
%
%t (true)
true or false
Printf("%t", true)
true
%b (binary)
Binary representation
Printf("%b", 5)
101
%c (char)
Unicode character
Printf("%c", 0x4E2D)
%d (decimal)
Decimal
Printf("%d", 0x12)
18
%o (octal)
Octal
Printf("%o", 10)
12
%q (quote)
Single-quoted character literal
Printf("%q", 0x4E2D)
'中'
%x
Hexadecimal, lowercase
Printf("%x", 13)
d
%X
Hexadecimal, uppercase
Printf("%X", 13)
D
%U (unicode)
Unicode format: U+1234
Printf("%U", 0x4E2D)
U+4E2D
%b
Binary exponent scientific notation
Printf("%b", 10.5)
5835037194198p-49
%e
Scientific notation
Printf("%e", 10.2)
1.020000e+01
%E
Scientific notation
Printf("%E", 10.2)
1.020000E+01
%f (float)
Decimal point, no exponent
Printf("%f", 10.2)
10.200000
%g
Compact format (%e or %f)
Printf("%g", 10.20)
10.2
%G
Compact format (%E or %f)
Printf("%G", 10.20)
10.2
%s (string)
String (string or []byte)
Printf("%s", []byte("Go"))
Go
%q
Double-quoted string
Printf("%q", "Go")
"Go"
%x
Hex, lowercase, two chars per byte
Printf("%x", "golang")
676f6c616e67
%X
Hex, uppercase, two chars per byte
Printf("%X", "golang")
676F6C616E67
%p (pointer)
Hexadecimal with 0x prefix
Printf("%p", &people)
0x4f57f0
+
Always print sign; ASCII-only for %+q
Printf("%+q", "中文")
"\u4e2d\u6587"
-
Pad with spaces on right (left-align)
#
Alternate format: 0 prefix for octal (%#o), 0x for hex (%#x), etc.
Printf("%#U", '中')
U+4E2D '中'
' '
Space for elided sign; spaces between bytes for hex
0
Pad with zeros; moves padding after sign for numbers
Reference:

String Formatting

Structs

Interfaces

Error Handling

Concurrency

Various Magic 🪄

...

Notes:
  1. Variadic parameter must be last
  1. Inside function, variadic parameter is slice type
  1. Types must match when expanding

Initialization Tricks

iota Enumerations

Type Aliases vs Type Definitions

make vs new

Go Gotchas and Important Notes

Slice Underlying Array Sharing

Map Randomness

String Bytes vs Characters

Interface nil Pitfall

Loop Variable Pitfall

Method Call Auto-conversion

Struct Embedding (Anonymous Fields)

init Functions and Package Imports

Vim in (not so) shortBasic Makefile
Loading...