Go语法 | 青训营笔记

79 阅读2分钟

Variables

When declare variables, we can use "var" or ":=" to declare.

var can automatically define the type of a variable, and you can definitely define it by yourself using syntax like:

var hello string = "helloworld"

Here, the string is the data type, var is the prefix of variable, and others are the contents of a variable. The type can also be assigned which based on the value you give to the var.

Contants

When declare constant values, we can use const, like this:

const hello = "helloworld"

const will scan your codes and define the type by itself.

if... else...

syntax: if, else if, else

if 7 % 2 == 0{
    fmt.println("7 is even")
} else {
    fmt.println("7 is odd")
}

You can not use the if...else... in one-liner style, Go forces you to add {} when using if...else...

Loop

There is no while or do...while syntax in Go, for is the only loop you can use.

syntax:

for j := 2; j < 10; j++ {
    fmt.println("hello")
}

You can use continue to move on or use break to jump out of the loop.

Switch

syntax:

switch variable {
case A:
    do something
case B:
    do something
default:
    do something
}

Go has built-in break function, so when variable meets its case, Go will break the whole switch.

Array

Array in Go has constant length, which makes it less useful in real-world coding.

syntax:

var a[5] int
a[4] = 10 // assign value

b := [5]int{1,2,3,4,5} // pre-define value

var 2d-array [3][3]int // 2-D array

Slice

Slice is a variable length array.

// To create a slice, we can do:
s := make([]string, 3)

// insert values
s[0] = "hello"

// append new value, we have to copy the original slice
s = append(s, "world")

Map

Most frequent used data structure in Go.

// Create a template map, in the following example, string is the type of key, int is the type of value
m := make(map[string]int)

// Create a map right away with keys and values
m2 := map[string]int{"first": 1, "second": 2}

// Delete one key-value pair
delete(m, "first")

Range

Traverse a map or array:

m := map[string]int{"a": 1, "b": 2}
for k, v := range m {
    fmt.println(k, v)
}

Function

syntax:

// here, "ok" is the error message for corresponding value
func exists(m map[string]string, k string) (v string, ok bool) {
    v, ok = m[k]
    return v, ok
}

Pointer

The most common use case of pointer in Go is to modify values for variables.

// Add 2 to an existing value

func add2ptr(n *int) {
    *n += 2
}

We make n a pointer. If we just use n+=2, we are actually adding 2 to a n's copy, not n itself.

when compile, we should use: add2ptr(&n) to make sure the value addition is working.

Struct

type user struct {
    name string
    password string
}

Error Handling

When define functions, we can add error attribute inside the function, which indicates that this attribute may return an error.

String Functions

We can check strings package for more. pkg.go.dev/strings

Time and Date

Go Time Package. pkg.go.dev/time

String Conversion

Go strconv Package.

Systems

Go os Package.