logo
💻 编程语言

Go

Go Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 编程语言🧭 Markdown 速查🏷️ 1 个标签
#golang
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

hello.go
GO
滚动查看更多
package main

import "fmt"

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

Run directly

SHELL
滚动查看更多
$ go run hello.go
Hello, world!

Or try it out in the Go repl

Variables
GO
滚动查看更多
var s1 string
s1 = "Learn Go!"

// declare multiple variables at once
var b, c int = 1, 2
var d = true

Short declaration

GO
滚动查看更多
s1 := "Learn Go!"        // string
b, c := 1, 2             // int
d := true                // bool

See: Basic types

Functions
GO
滚动查看更多
package main

import "fmt"

// The entry point of the programs
func main() {
    fmt.Println("Hello world!")
    say("Hello Go!")
}

func say(message string) {
    fmt.Println("You said: ", message)
}

See: Functions

Comments
GO
滚动查看更多
// Single line comment

/* Multi-
 line comment */
If statement
GO
滚动查看更多
if true {
    fmt.Println("Yes!")
}

See: Flow control

Go Basic types

Strings
GO
滚动查看更多
s1 := "Hello" + "World"

s2 := `A "raw" string literal
can include line breaks.`

// Outputs: 10
fmt.Println(len(s1))

// Outputs: Hello
fmt.Println(string(s1[0:5]))

Strings are of type string.

Numbers
GO
滚动查看更多
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

GO
滚动查看更多
x := 5
x++
fmt.Println("x + 4 =", x + 4)
fmt.Println("x * 4 =", x * 4)

See: More Operators

Booleans
GO
滚动查看更多
isTrue   := true
isFalse  := false

Operators

GO
滚动查看更多
fmt.Println(true && true)   // true
fmt.Println(true && false)  // false
fmt.Println(true || true)   // true
fmt.Println(true || false)  // true
fmt.Println(!true)          // false

See: More Operators

Arrays
GO
滚动查看更多
┌────┬────┬────┬────┬─────┬─────┐
| 2  | 3  | 5  | 7  | 11  | 13  |
└────┴────┴────┴────┴─────┴─────┘
  0    1    2    3     4     5

GO
滚动查看更多
primes := [...]int{2, 3, 5, 7, 11, 13}
fmt.Println(len(primes)) // => 6

// Outputs: [2 3 5 7 11 13]
fmt.Println(primes)

// Same as [:3], Outputs: [2 3 5]
fmt.Println(primes[0:3])

GO
滚动查看更多
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

GO
滚动查看更多
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
GO
滚动查看更多
func main () {
  b := *getPointer()
  fmt.Println("Value is", b)
}
GO
滚动查看更多
func getPointer () (myPointer *int) {
  a := 234
  return &a
}
GO
滚动查看更多
a := new(int)
*a = 234

See: Pointers

Slices
GO
滚动查看更多
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}

See also: Slices example

Constants
GO
滚动查看更多
const s string = "constant"
const Phi = 1.618
const n = 500000000
const d = 3e20 / n
fmt.Println(d)
Type conversions
GO
滚动查看更多
i := 90
f := float64(i)
u := uint(i)

// Will be equal to the character Z
s := string(i)

How to get int string?

GO
滚动查看更多
i := 90

// need import "strconv"
s := strconv.Itoa(i)
fmt.Println(s) // Outputs: 90

Go Strings

Strings function
GO
滚动查看更多
package main

import (
	"fmt"
	s "strings"
)

func main() {
    /* Need to import strings as s */
	fmt.Println(s.Contains("test", "e"))

    /* Build in */
    fmt.Println(len("hello"))  // => 5
    // Outputs: 101
	fmt.Println("hello"[1])
    // Outputs: e
	fmt.Println(string("hello"[1]))

}
fmt.Printf
GO
滚动查看更多
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")
}

See also: fmt

Function examples
ExampleResult
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

Go Flow control

Conditional
GO
滚动查看更多

a := 10

if a > 20 {
    fmt.Println(">")
} else if a < 20 {
    fmt.Println("<")
} else {
    fmt.Println("=")
}
Statements in if
GO
滚动查看更多
x := "hello go!"

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


GO
滚动查看更多

if _, err := doThing(); err != nil {
    fmt.Println("Uh oh")
}
Switch
GO
滚动查看更多
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")
}

See: Switch

For loop
GO
滚动查看更多
for i := 0; i <= 10; i++ {
  fmt.Println("i: ", i)
}
For-Range loop
GO
滚动查看更多
nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
    sum += num
}
fmt.Println("sum:", sum)
While loop
GO
滚动查看更多
i := 1
for i <= 3 {
    fmt.Println(i)
    i++
}
Continue keyword
GO
滚动查看更多
for i := 0; i <= 5; i++ {
    if i % 2 == 0 {
        continue
    }
    fmt.Println(i)
}
Break keyword
GO
滚动查看更多
for {
    fmt.Println("loop")
    break
}

Go Structs & Maps

Defining
GO
滚动查看更多
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
}

See: Structs

Literals
GO
滚动查看更多
v := Vertex{X: 1, Y: 2}
// Field names can be omitted
v := Vertex{1, 2}
// Y is implicit
v := Vertex{X: 1}

You can also put field names.

Maps
GO
滚动查看更多
m := make(map[string]int)
m["k1"] = 7
m["k2"] = 13
fmt.Println(m) // => map[k1:7 k2:13]

v1 := m["k1"]
fmt.Println(v1)     // => 7
fmt.Println(len(m)) // => 2

delete(m, "k2")
fmt.Println(m) // => map[k1:7]

_, prs := m["k2"]
fmt.Println(prs) // => false

n := map[string]int{"foo": 1, "bar": 2}
fmt.Println(n) // => map[bar:2 foo:1]
Pointers to structs
GO
滚动查看更多
v := &Vertex{1, 2}
v.X = 2

Doing v.X is the same as doing (*v).X, when v is a pointer.

Go Functions

Multiple arguments
GO
滚动查看更多
func plus(a int, b int) int {
    return a + b
}
func plusPlus(a, b, c int) int {
    return a + b + c
}
fmt.Println(plus(1, 2))
fmt.Println(plusPlus(1, 2, 3))
Multiple return
GO
滚动查看更多
func vals() (int, int) {
    return 3, 7
}

a, b := vals()
fmt.Println(a)    // => 3
fmt.Println(b)    // => 7
Function literals
GO
滚动查看更多
r1, r2 := func() (string, string) {
    x := []string{"hello", "cheatsheets.zip"}
    return x[0], x[1]
}()

// => hello cheatsheets.zip
fmt.Println(r1, r2)
Naked returns
GO
滚动查看更多
func split(sum int) (x, y int) {
  x = sum * 4 / 9
  y = sum - x
  return
}

x, y := split(17)
fmt.Println(x)   // => 7
fmt.Println(y)   // => 10

Note that using naked returns hurts readability.

Variadic functions
GO
滚动查看更多
func sum(nums ...int) {
    fmt.Print(nums, " ")
    total := 0
    for _, num := range nums {
        total += num
    }
    fmt.Println(total)
}
sum(1, 2)     //=> [1 2] 3
sum(1, 2, 3)  // => [1 2 3] 6

nums := []int{1, 2, 3, 4}
sum(nums...)  // => [1 2 3 4] 10
init function
GO
滚动查看更多
import --> const --> var --> init()

GO
滚动查看更多
var num = setNumber()

func setNumber() int {
    return 42
}
func init() {
    num = 0
}
func main() {
    fmt.Println(num) // => 0
}
Functions as values
GO
滚动查看更多
func main() {
    // assign a function to a name
    add := func(a, b int) int {
        return a + b
    }
    // use the name to call the function
    fmt.Println(add(3, 4)) // => 7
}
Closures 1
GO
滚动查看更多
func scope() func() int{
    outer_var := 2
    foo := func() int {return outer_var}
    return foo
}

// Outpus: 2
fmt.Println(scope()())
Closures 2
GO
滚动查看更多
func outer() (func() int, int) {
    outer_var := 2
    inner := func() int {
        outer_var += 99
        return outer_var
    }
    inner()
    return inner, outer_var
}
inner, val := outer()
fmt.Println(inner()) // => 200
fmt.Println(val)     // => 101

Go Packages

Importing
GO
滚动查看更多
import "fmt"
import "math/rand"

Same as

GO
滚动查看更多
import (
  "fmt"        // gives fmt.Println
  "math/rand"  // gives rand.Intn
)

See: Importing

Aliases
GO
滚动查看更多
import r "math/rand"

GO
滚动查看更多
import (
    "fmt"
    r "math/rand"
)

GO
滚动查看更多
r.Intn()
Packages
GO
滚动查看更多
package main

// An internal package may be imported only by another package
// that is inside the tree rooted at the parent of the internal directory
package internal

See: Internal packages

Exporting names
GO
滚动查看更多
// Begin with a capital letter
func Hello () {
  ···
}

See: Exported names

Go Concurrency

Goroutines
GO
滚动查看更多
package main

import (
	"fmt"
	"time"
)

func f(from string) {
	for i := 0; i < 3; i++ {
		fmt.Println(from, ":", i)
	}
}

func main() {
	f("direct")
	go f("goroutine")

	go func(msg string) {
		fmt.Println(msg)
	}("going")

	time.Sleep(time.Second)
	fmt.Println("done")
}

See: Goroutines, Channels

WaitGroup
GO
滚动查看更多
package main

import (
	"fmt"
	"sync"
	"time"
)

func w(id int, wg *sync.WaitGroup) {
	defer wg.Done()
	fmt.Printf("%d starting\n", id)

	time.Sleep(time.Second)
	fmt.Printf("%d done\n", id)
}

func main() {
	var wg sync.WaitGroup
	for i := 1; i <= 5; i++ {
		wg.Add(1)
		go w(i, &wg)
	}
	wg.Wait()
}

See: WaitGroup

Closing channels
GO
滚动查看更多
ch <- 1
ch <- 2
ch <- 3
close(ch) // Closes a channel

GO
滚动查看更多
// Iterate the channel until closed
for i := range ch {
  ···
}

GO
滚动查看更多
// Closed if `ok == false`
v, ok := <- ch

See: Range and close

Buffered channels
GO
滚动查看更多
ch := make(chan int, 2)
ch <- 1
ch <- 2
ch <- 3
// fatal error:
// all goroutines are asleep - deadlock

See: Buffered channels

Go Error control

Deferring functions
GO
滚动查看更多
func main() {
  defer func() {
    fmt.Println("Done")
  }()
  fmt.Println("Working...")
}
Lambda defer
GO
滚动查看更多
func main() {
  var d = int64(0)
  defer func(d *int64) {
    fmt.Printf("& %v Unix Sec\n", *d)
  }(&d)
  fmt.Print("Done ")
  d = time.Now().Unix()
}

The defer func uses current value of d, unless we use a pointer to get final value at end of main.

Defer
GO
滚动查看更多
func main() {
  defer fmt.Println("Done")
  fmt.Println("Working...")
}

See: Defer, panic and recover

Go Methods

Receivers
GO
滚动查看更多
type Vertex struct {
  X, Y float64
}
GO
滚动查看更多
func (v Vertex) Abs() float64 {
  return math.Sqrt(v.X * v.X + v.Y * v.Y)
}
GO
滚动查看更多
v := Vertex{1, 2}
v.Abs()

See: Methods

Mutation
GO
滚动查看更多
func (v *Vertex) Scale(f float64) {
  v.X = v.X * f
  v.Y = v.Y * f
}
GO
滚动查看更多
v := Vertex{6, 12}
v.Scale(0.5)
// `v` is updated

See: Pointer receivers

Go Interfaces

A basic interface
GO
滚动查看更多
type Shape interface {
  Area() float64
  Perimeter() float64
}
Struct
GO
滚动查看更多
type Rectangle struct {
  Length, Width float64
}

Struct Rectangle implicitly implements interface Shape by implementing all of its methods.

Methods
GO
滚动查看更多
func (r Rectangle) Area() float64 {
  return r.Length * r.Width
}

func (r Rectangle) Perimeter() float64 {
  return 2 * (r.Length + r.Width)
}

The methods defined in Shape are implemented in Rectangle.

Interface example
GO
滚动查看更多
func main() {
  var r Shape = Rectangle{Length: 3, Width: 4}
  fmt.Printf("Type of r: %T, Area: %v, Perimeter: %v.", r, r.Area(), r.Perimeter())
}

Go generics

example 1
GO
滚动查看更多
// comparable represents types that can be compared.
type comparable interface {
	int | float64 | string
}

// Max returns the maximum of two comparable values.
func Max[T comparable](a, b T) T {
	if a > b {
		return a
	}
	return b
}

func main() {
	// Find the maximum of two integers.
	maxInt := Max(10, 20)
	fmt.Println("Max integer:", maxInt)

	// Find the maximum of two floats.
	maxFloat := Max(3.14, 2.71)
	fmt.Println("Max float:", maxFloat)

	// Find the maximum of two strings.
	maxString := Max("apple", "banana")
	fmt.Println("Max string:", maxString)
}

example 2
GO
滚动查看更多

// Pair[T, U] represents a generic pair of values.
type Pair[T, U any] struct {
	First  T
	Second U
}

func main() {
	pair := Pair[int, string]{First: 42, Second: "hello"}

	fmt.Println("First:", pair.First)
	fmt.Println("Second:", pair.Second)

	// Print the types of the values in the pair.
	fmt.Println("Type of First:", reflect.TypeOf(pair.First))
	fmt.Println("Type of Second:", reflect.TypeOf(pair.Second))
}

Go File I/O

Reading a File

Read the contents of a file into memory.

GO
滚动查看更多
data, err := os.ReadFile("file.txt")
if err != nil {
    log.Fatal(err)
}
Writing to a File

Write data to a file with specific permissions (e.g., 0644 for read-write).

GO
滚动查看更多
err := os.WriteFile("file.txt", []byte("Hello, Go!"), 0644)

Go Testing

Unit Test

Unit Test: A basic unit test follows the below naming convention and uses t.Errorf() to report failures.

GO
滚动查看更多
func TestAdd(t *testing.T) {
    result := add(2, 3)
    if result != 5 {
        t.Errorf("Expected 5, got %d", result)
    }
}
Unit Test

Benchmark: Benchmark tests measure performance, using b.N to control iterations.

GO
滚动查看更多
func BenchmarkAdd(b *testing.B) {
    for i := 0; i < b.N; i++ {
        add(1, 2)
    }
}

Go JSON Handling

JSON Encoding

Convert a Go struct to JSON.

GO
滚动查看更多
data, _ := json.Marshal(person)
JSON Decoding

Parse JSON data into a Go struct.

GO
滚动查看更多
json.Unmarshal(data, &person)

Go Server

net/http (Standard Library)
GO
滚动查看更多
package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello from net/http!")
}

func main() {

    http.HandleFunc("/", handler)

    http.ListenAndServe(":8080", nil)
}

gorilla/mux
GO
滚动查看更多
package main

import (
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
)

func main() {

    r := mux.NewRouter()

    r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello from Gorilla Mux!")
    })

    http.ListenAndServe(":8081", r)
}

chi
GO
滚动查看更多
package main

import (
    "fmt"
    "net/http"
    "github.com/go-chi/chi/v5"
)

func main() {

    r := chi.NewRouter()

    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello from Chi!")
    })

    http.ListenAndServe(":8082", r)
}
gin
GO
滚动查看更多
package main

import (
    "github.com/gin-gonic/gin"
)

func main() {

    r := gin.Default()

    r.GET("/", func(c *gin.Context) {
        c.String(200, "Hello from Gin!")
    })

    r.Run(":8083")
}
fiber
GO
滚动查看更多
package main

import (
    "github.com/gofiber/fiber/v3"
)

func main() {

    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello from Fiber!")
    })

    app.Listen(":8084")
}
beego
GO
滚动查看更多
package main

import (
    "github.com/beego/beego/v2/server/web"
)

func main() {

    web.Get("/", func(ctx *web.Context) {
        ctx.Output.Body([]byte("Hello from Beego!"))
    })

    web.Run()
}

Miscellaneous

Keywords
  • break
  • default
  • func
  • interface
  • select
  • case
  • defer
  • go
  • map
  • struct
  • chan
  • else
  • goto
  • package
  • switch
  • const
  • fallthrough
  • if
  • range
  • type
  • continue
  • for
  • import
  • return
  • var
  • iota

{.cols-3 .marker-none}

Operators and punctuation
+&+=&=&&==!=()
-<code>|</code>-=<code>|=</code><code>||</code><<=[]
<code>*</code>^<code>*=</code>^=<->>={}
/<</=<<=++=:=,;
%>>%=>>=--!....:
&^&^=

Also see

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572