NOTE

Learning Go

Go characteristics, comparison with Java, language basics, data structures, concurrency, memory management, build process, performance tools, and references.

Go2 min readhistorical

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. Needs to be assembled by yourself Complete. Business development 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

Go 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 break is 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 break is 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

select.md

3.4. defer

defer.md

3.5. panic and recover

panic and recover.md

3.6. make and new

make vs new.md

4. Type System

Type System

5. Data Structures

5.1. string

string.md

5.2. array

array.md

5.3. slice

slice.md

5.4. map

map.md

5.5. struct

struct.md

6. Functions

function.md

7. Interfaces

interface.md

8. Packages

package.md

9. Dependency Management

go.mod.md

10. Concurrency

concurrent.md

11. Reflection

reflection.md

12. Errors

error.md

13. Unit Testing

unittest.md

14. benchmark

benchmark.md

15. Debugging

dlv.md

16. Memory Management

Memory Management Memory Alignment

17. GC

GC.md

18. Pointers

unsafe.md

19. Build Process

Go Build Process Plan 9 Assembly

20. Performance Optimization

pprof.md trace.md

21. Core Dump

Debugging Go Core Dumps - SegmentFault Analyzing Go Core Dumps - CodeLeading

22. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub