Golang 备忘清单

该备忘单提供了帮助您使用 Golang 的基本语法和方法。

入门

package main
import "fmt"
func main() {
    fmt.Println("Hello, world!")
}

直接运行

$ go run hello.go
Hello, world!

或者在 Go repl 中尝试一,go 命令参考

变量

var s1 string
s1 = "Learn Go!"
// 一次声明多个变量
var b, c int = 1, 2
var d = true
// 匿名赋值
_ , e = 10, 20 

简短声明

s1 := "Learn Go!"        // string
b, c := 1, 2             // int
d := true                // bool

参见:基本类型

函数

package main
import "fmt"
// 程序的入口点
func main() {
  fmt.Println("Hello world!")
  say("Hello Go!")
}
func say(message string) {
  fmt.Println("You said: ", message)
}

参见:函数(Functions)

注释

// 单行注释
/* 这是
多行注释 */

if 语句

if true {
  fmt.Println("Yes!")
}

参见:条件控制

Golang 基本类型

字符串 Strings

s1 := "Hello" + "World"
s2 := `A "raw" string literal
can include line breaks.`
// 输出:10
fmt.Println(len(s1))
// 输出:Hello
fmt.Println(string(s1[0:5]))

字符串的类型为 字符串

数字 Numbers

num := 3             // int
num := 3.            // float64
num := 3 + 4i        // complex128
num := byte('a')     // byte (alias: uint8)
var u uint = 7       // uint (unsigned)
var p float32 = 22.7  // 32-bit float

操作符 Operators

x := 5
x++
fmt.Println("x + 4 =", x + 4)
fmt.Println("x * 4 =", x * 4) 

参见:更多操作符

布尔值 Booleans

isTrue   := true
isFalse  := false

操作符

fmt.Println(true && true)   // true 
fmt.Println(true && false)  // false
fmt.Println(true || true)   // true
fmt.Println(true || false)  // true
fmt.Println(!true)          // false

参见:更多操作符

数组 Arrays

┌────┬────┬────┬────┬─────┬─────┐
| 2  | 3  | 5  | 7  | 11  | 13  |
└────┴────┴────┴────┴─────┴─────┘
  0    1    2    3     4     5

primes := [...]int{2, 3, 5, 7, 11, 13}
fmt.Println(len(primes)) // => 6
// 输出:[2 3 5 7 11 13]
fmt.Println(primes)
// 与 [:3] 相同,输出:[2 3 5]
fmt.Println(primes[0:3])

var a [2]string
a[0] = "Hello"
a[1] = "World"
fmt.Println(a[0], a[1]) //=> Hello World
fmt.Println(a)   // => [Hello World]

2d array

var twoDimension [2][3]int
for i := 0; i < 2; i++ {
    for j := 0; j < 3; j++ {
        twoDimension[i][j] = i + j
    }
}
// => 2d:  [[0 1 2] [1 2 3]]
fmt.Println("2d: ", twoDimension)

指针(Pointers)

func main () {
  b := *getPointer()
  fmt.Println("Value is", b)
}

func getPointer () (myPointer *int) {
  a := 234
  return &a
}
//申明指针的时候,如果没有指向某个变量,默认值为nil
//不能直接进行操作,包括读写
var p *int
*p = 123      // panic   nil pointer

//而用new返回的是有默认值的指针, 为数据类型的默认值
func main(){
  //有一块内存存放了10,它的地址由系统自动分配,别名是a
  a := 10
  //内存存放的10变成了20
  a = 20
  var p *int
  p = &a   //或者直接写 p := &a
  //上面的p是一个指针,通过 *p 的方式同样可以访问 变量a指向 的内存

  /*当你动态申请内存的时候,指针的存在意义之一就被体现出来了*/ 
  ptr := new(int)   
  //申请了一块内存空间,没有办法指定别名,new()返回内存地址,用指针接收
  //此时并没有变量能直接指向这块内存,所以只能通过内存地址来访问
}

参见:指针(Pointers)

切片(Slices)

s := make([]string, 3)
s[0] = "a"
s[1] = "b"
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println(s)
fmt.Println(s[1])
fmt.Println(len(s))
fmt.Println(s[1:3])
slice := []int{2, 3, 4}

另见:切片示例

常量(Constants)

const s string = "constant"
const Phi = 1.618
const n = 500000000
const d = 3e20 / n

常量声明可以使用 iota常量生成器 初始化,它用于 生成一组以相似规则初始化的常量,但是不用每行都 写一遍初始化表达式。 注意:

  1. 在一个const声明语句中,在第一个声明的常量所在的行,iota被置为0,然后在每一个有常量声明的行加一。
  2. 写在同一行的值是相同的
const (
    a = iota
    b
    c
)
// a = 0, b = 1, c = 2

类型转换

Go语言中不允许隐式转换,所有类型转换必须显式声明(强制转换),而且转换只能发生在两种相互兼容的类型之间。

i := 90
f := float64(i)
u := uint(i)
// 将等于字符Z
s := string(i)

如何获取int字符串?

i := 90
// 需要导入“strconv”
s := strconv.Itoa(i)
fmt.Println(s) // Outputs: 90

Golang 字符串

字符串函数

package main
import (
        "fmt"
        s "strings"
)
func main() {
    /* 需要将字符串导入为 s */
        fmt.Println(s.Contains("test", "e"))
    /* 内置 */
    fmt.Println(len("hello"))  // => 5
    // 输出: 101
        fmt.Println("hello"[1])
    // 输出: e
        fmt.Println(string("hello"[1]))
}

fmt.Printf

package main
import (
        "fmt"
        "os"
)
type point struct {
        x, y int
}
func main() {
        p := point{1, 2}
        fmt.Printf("%v\n", p)                        // => {1 2}
        fmt.Printf("%+v\n", p)                       // => {x:1 y:2}
        fmt.Printf("%#v\n", p)                       // => main.point{x:1, y:2}
        fmt.Printf("%T\n", p)                        // => main.point
        fmt.Printf("%t\n", true)                     // => TRUE
        fmt.Printf("%d\n", 123)                      // => 123
        fmt.Printf("%b\n", 14)                       // => 1110
        fmt.Printf("%c\n", 33)                       // => !
        fmt.Printf("%x\n", 456)                      // => 1c8
        fmt.Printf("%f\n", 78.9)                     // => 78.9
        fmt.Printf("%e\n", 123400000.0)              // => 1.23E+08
        fmt.Printf("%E\n", 123400000.0)              // => 1.23E+08
        fmt.Printf("%s\n", "\"string\"")             // => "string"
        fmt.Printf("%q\n", "\"string\"")             // => "\"string\""
        fmt.Printf("%x\n", "hex this")               // => 6.86578E+15
        fmt.Printf("%p\n", &p)                       // => 0xc00002c040
        fmt.Printf("|%6d|%6d|\n", 12, 345)           // => |    12|   345|
        fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)     // => |  1.20|  3.45|
        fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)   // => |1.20  |3.45  |
        fmt.Printf("|%6s|%6s|\n", "foo", "b")        // => |   foo|     b|
        fmt.Printf("|%-6s|%-6s|\n", "foo", "b")      // => |foo   |b     |
        s := fmt.Sprintf("a %s", "string")
        fmt.Println(s)
        fmt.Fprintf(os.Stderr, "an %s\n", "error")
}

另见:fmt

函数实例

实例Result
Contains("test", "es")true
Count("test", "t")2
HasPrefix("test", "te")true
HasSuffix("test", "st")true
Index("test", "e")1
Join([]string{"a", "b"}, "-")a-b
Repeat("a", 5)aaaaa
Replace("foo", "o", "0", -1)f00
Replace("foo", "o", "0", 1)f0o
Split("a-b-c-d-e", "-")[a b c d e]
ToLower("TEST")test
ToUpper("test")TEST

Golang 条件控制

有条件的

a := 10
if a > 20 {
    fmt.Println(">")
} else if a < 20 {
    fmt.Println("<")
} else {
    fmt.Println("=")
}

if 中的语句

x := "hello go!"
if count := len(x); count > 0 {
    fmt.Println("Yes")
}

if _, err := doThing(); err != nil {
    fmt.Println("Uh oh")
}

Switch

x := 42.0
switch x {
  case 0:
  case 1, 2:
      fmt.Println("Multiple matches")
  case 42:   // Don't "fall through".
      fmt.Println("reached")
  case 43:
      fmt.Println("Unreached")
  default:
      fmt.Println("Optional")
}

参见:Switch

For loop

for i := 0; i <= 10; i++ {
  fmt.Println("i: ", i)
}

对于 Range 循环

nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
  sum += num
}
fmt.Println("sum:", sum)

For 循环

i := 1
for i <= 3 {
  fmt.Println(i)
  i++
}

Continue 关键字

for i := 0; i <= 5; i++ {
  if i % 2 == 0 {
      continue
  }
  fmt.Println(i)
}

Break 关键字

for {
  fmt.Println("loop")
  break
}

Golang 结构和Maps

定义

package main
import (
        "fmt"
)
type Vertex struct {
        X int
        Y int
}
func main() {
        v := Vertex{1, 2}
        v.X = 4
        fmt.Println(v.X, v.Y) // => 4 2
}

参见:结构(Structs)

字面量

v := Vertex{X: 1, Y: 2}
// Field names can be omitted
v := Vertex{1, 2}
// Y is implicit
v := Vertex{X: 1}

您还可以输入字段