NOTE
Learning Go
Go is a statically typed compiled language. These notes connect its characteristics with environment setup, basic syntax, and runtime topics.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Characteristics of Golang
- Compiled, so it does not require a virtual machine
- Statically typed
- Has GC; memory management is fully automatic
1.1. Golang vs Java
| Go | Java | |
|---|---|---|
| Virtual machine | No | Yes |
| Cross-platform | Supported; depends on compiling for different platforms | Supported; depends on the JVM: compile once, run anywhere |
| Type | Compiled | Interpreted |
| Memory management | GC, automatic | GC, automatic |
| Programming paradigms | Procedural, object-oriented, functional, message-oriented | Object-oriented |
| Ecosystem | Incomplete; you need to assemble the components yourself | Mature; application development is based on Spring |
| Go’s memory management is similar to an operating system’s virtual address space, dividing memory into equal-sized blocks. |
2. Environment Setup
3. Keywords
3.1. for
3.1.1. Different Forms of the for Loop
3.1.1.1. Classic for Loop
func TestFor1(t *testing.T) {
//for (init;before;after)
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
// Output
45
3.1.1.2. while Loop
func TestFor2(t *testing.T) {
//while(xxx)
x := 0
for x < 10 {
fmt.Println(x)
x = x + 1
}
}
// Output
0
1
2
3
4
5
6
7
8
9
3.1.1.3. Infinite Loop
func TestFor3(t *testing.T) {
//for(;;)
for {
fmt.Println("test")
}
}
3.1.1.4. Enhanced for Loop
func TestFor4(t *testing.T) {
// Iterate over arrays, slices, strings, or maps, or receive messages from a channel
oldMap := map[int]string{
1: "a",
2: "b",
}
newMap := make(map[int]string)
for k, v := range oldMap {
newMap[k] = v
}
fmt.Println(oldMap, newMap)
}
// Output
map[1:a 2:b] map[1:a 2:b]
3.1.1.4.1. Concurrency Issue
The enhanced for loop iterates using the same address, which can cause problems under concurrency.
func TestFor5(t *testing.T) {
arrays := []uint64{1,2,4}
for _, val := range arrays {
fmt.Println(&val, val)
}
}
// Output
0xc00000a310 1
0xc00000a310 2
0xc00000a310 4
- Solution: use a local variable.
func TestFor6(t *testing.T) {
arrays := []uint64{1,2,4}
for _, val := range arrays {
tmpVal := val
fmt.Println(&tmpVal, tmpVal)
}
}
// Output
0xc00000a310 1
0xc00000a320 2
0xc00000a330 4
3.2. switch
- No
breakis required.
func TestSwitch1(t *testing.T) {
unhex('0')
}
func unhex(c byte) {
switch {
case c >= '0':
fmt.Println("c>=0")
case c == '0':
fmt.Println("c==0")
default:
fmt.Println("c<0")
}
}
// Output
c>=0
- Adding
breakis also fine.
func TestSwitch2(t *testing.T) {
unhex2('0')
}
func unhex2(c byte) {
switch c {
case '0':
fmt.Println("0")
case '1', '2':
fmt.Println("1")
break // adding break manually is also OK; qqqqq below will not be printed
fmt.Println("qqqqqqqqqqqqqq")
default:
fmt.Println("others")
}
}
// Output
0
3.3. select
3.4. defer
3.5. panic and recover
3.6. make and new
4. Type System
5. Data Structures
5.1. string
5.2. array
5.3. slice
5.4. map
5.5. struct
6. Functions
7. Interfaces
8. Packages
9. Dependency Management
10. Concurrency
11. Reflection
12. Errors
13. Unit Testing
14. benchmark
15. Debugging
16. Memory Management
Memory Management Memory Alignment
17. GC
18. Pointers
19. Build Process
Go Build Process Plan 9 Assembly
20. Performance Optimization
21. Core Dump
Debugging Go Core Dumps - SegmentFault Analyzing Go Core Dumps - CodeLeading
22. References
- Documentation - The Go Programming Language
- A Tour of Go
- Effective Go - The Go Programming Language
- The Go Programming Language Specification - The Go Programming Language
- Packages - The Go Programming Language
- How to Study Go Internals · Deep Dive into Go
- Go Language Design and Implementation
- Introduction · Advanced Go Programming
- Blog Posts, Page 2 - CNBlogs
- Programming Languages | qcrao
- a-journey-with-go
- https://mp.weixin.qq.com/mp/homepage?__biz=Mzg5NjIwNzIxNQ==&hid=3&sn=763be099a57984aae1baef3f45b38db7&scene=1&devicetype=Windows+10+x64&version=6302018f&lang=zh_CN&nettype=cmnet&ascene=1&session_us=gh_b3e3966468b1&wx_header=1&uin=&key=&fontgear=2
- Go vs Java: Which Is the Final Answer? - Zhihu
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub