Technology Sharing

Introduction to Go Language (I)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Go Modules dependency package search mechanism
  • Downloaded third-party dependencies are stored in $GOPATH/pkg/mod Down
  • The executable files generated by go install are stored in $GOPATH/binDown
  • Dependency search order:
    • Work list
    • $GOPATH/pkg/mod
    • $GOPATH/src

1. Go Language Basics

1. Identifiers and Keywords

1.1 Naming

The naming of go variables, constants, custom types, packages, and functions must follow the following rules:

The first character can be任意Unicode字符or下划线

The part other than the first character can be a Unicode character, an underscore, or a number

No limit on the length of the name

In theory, a name can contain Chinese characters, or even be entirely Chinese characters, but in practice, don't do this.

1.2 Keywords
breakdefaultfuncinterfaceselect
casedefergomapstruct
chanelsegotopackageswitch
constifrangetypecontinue
forimportreturnfallthroughvar
1.3 Reserved Words

•constant

true false iota nil

•type of data

int int8 int16 int32 int64

uint uint8 uint16 uint32 uint64

float32 float64

complex128 complex64

byte rune string

bool error uintptr

•function

make len cap new append copy

close delete complex real imag panic recover

1.4 Variables
1.4.1 Variable Types
typeGo variable typesfmt output
Integerint int8 int16 int32 int64 uint uint8 uint16 uint32 uint64%d
Floating pointfloat32 float64%f %e %g
pluralcomplex128 complex64%v
Booleanbool%t
pointeruintptr%d
Referencesmap slice channel%v
bytebyte%d
Any characterrune%d
Stringstring%s
mistakeerror%v
1.4.2 Variable Initialization
  • If not explicitly initialized after declaration, numeric types are initialized to O, strings are initialized to an empty string, and Boolean types are initialized to false.

    Reference types, functions, pointers, and interfaces are initialized to nil

    • var a string=“china”
    • var a,b int=3,7
    • var a = "china", type inference
    • var a,b=“china”, 7
  • Variables inside a function (non-global variables) can be declared and initialized using:

    • a:=3
  • Underscores indicate anonymous variables

    • _=2+4
  • Anonymous variables do not occupy namespace and do not allocate memory, so they can be reused

1.5 Constants

Constants must be assigned values ​​when they are defined, and their values ​​cannot be changed during program execution.

const PI float32=3.14

const(
	PI=3.14
	E=2.71
)

const(
  a=100
  b	//100,跟上一行的值相同
  c	//100,跟上一行的值相同
)

const(
	a = iota	//0
  b					//1
  c					//2
  d					//3
)

const(
	a = iota	//0
  b					//1
  _					//2
  d					//3
)

const(
	a = iota	//0
  b	= 30		//30
  c	= iota	//2
  d					//3
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
1.6 Literals

Literal: The value appears directly without the variable name. Literals of basic types are equivalent to constants.

Different data types cannot be compared, except for literals.

fmt.Printf("9tn"04 ==4.00//用到了整型字面量和浮点型字面量
fmt.Printf("%vn".4i//虚数字面量0.4i
fmt.Printf("%tn", "u4f17'=='//Unicode和rune字面量
fmt.Printf("Hello nWorldn!n"//字符串字面量
  • 1
  • 2
  • 3
  • 4
1.7 Variable Scope
Global variables
var (
  A=3	//全局变量,大写字母开头,所有地方都可以访问,跨package访问时需要带上package名称
	b=4	//全局变量,小写字母开头,本package内都可以访问
  • 1
  • 2
  • 3
  • 4
Local variables
func fool{
	b:=5 //局部变量,仅本函数内可以访问。内部声明的变量可以跟外部声明的变量有冲突,以内部的为准
  
	{
  	b:=6	//仅小圈定的作用域内可以访问,可以跟外部的变量有冲突
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
1.8 Comments and godoc
1.8.1 Comment Format
  • Single line comment. Start with //
  • Multi-line comments. Multiple consecutive lines start with //, or use /* before a paragraph and */ at the end of a paragraph.
  • There cannot be blank lines between multi-line comments.
  • NOTE: Attention, TODO: Need to be optimized in the future, Deprecated: Variables or functions are strongly recommended not to be used
  • Add indentation before the comment line to write go code
1.8.2 Position of comments
  • Package comments. Above package xxx. A package only needs to have package comments in one place, usually a doc.go file.

    There is only one line packagexxx and comments about the package

  • Struct comment. Above type xxx struct

  • Function comments. Above func xxx()

  • Line comment. Above or to the right of the line

1.8.3 go doc
  • go doc is a command that comes with go
  • go doc entrance class/util
1.8.4 godoc
  • godoc can export web version of annotation documentation for project code
    • You need to install go first get golang.org/x/tools/cmd/godoc
    • Start http:godoc -http=:6060
    • Use a browser to access: http://127.0.0.1:6060/pkg/go-course/entrance class