Go Tips

警告
本文最后更新于 2020-11-03,文中内容可能已过时。

https://pbs.twimg.com/media/EbNIJiiU0AATDBP?format=jpg&name=large

https://pbs.twimg.com/media/EcBiO8MU8AIU4uI?format=jpg&name=small

https://pbs.twimg.com/media/EdERVjDVcAAuqyT?format=png&name=large

https://pbs.twimg.com/media/EaVFqMRUYAEJShZ?format=jpg&name=large

https://pbs.twimg.com/media/EaAAFckUYAMfiTg?format=jpg&name=small

https://pbs.twimg.com/media/EZmd2icU0AATqsj?format=jpg&name=small

https://pbs.twimg.com/media/EYtDAGrVAAAzxDm?format=png&name=900x900

https://pbs.twimg.com/media/EYZiVS2VAAA4VpZ?format=jpg&name=large

https://pbs.twimg.com/media/EXlYC_7XkAIKxdf?format=jpg&name=large

可以在格式说明符中使用“ 0”标志来填充前导零的数字。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import "fmt"

func main() {
	fmt.Printf("%05d\n", 12)
	fmt.Printf("%05d\n", 123)
	fmt.Printf("%05d\n", 1234)
	fmt.Printf("%05d\n", 12345)
}

如果两个结构都具有相同的基础类型,则可以将它们转换为另一个结构。即使它们具有不同的struct标签,此方法也有效。

 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
package main

import "fmt"

type Person struct {
	Name string `json:"fullName"`
}

type Employee struct {
	Name string `json:"firstName"`
}

func main() {
	p := Person{
		Name: "Go",
	}

	var e Employee
	e = Employee(p)
	fmt.Printf("%+v", e)
}

Output
----------
{Name:Go}

“调试”包的“打印堆栈”功能可用于打印调用goroutine的堆栈跟踪。这在调试过程中跟踪函数调用的路径非常有用。

 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
package main

import (
	"runtime/debug"
)

func hello2() {
	debug.PrintStack()
}

func hello1() {
	hello2()
}

func main() {
	hello1()
}

Output
----------
goroutine 1 [running]:
runtime/debug.Stack(0xc000034778, 0xc000066f78, 0x404845)
	/usr/local/go/src/runtime/debug/stack.go:24 +0x9f
runtime/debug.PrintStack()
	/usr/local/go/src/runtime/debug/stack.go:16 +0x25
main.hello2(...)
	/home/kira/workspace/demo/http01/demo1.go:8
main.hello1(...)
	/home/kira/workspace/demo/http01/demo1.go:12
main.main()
	/home/kira/workspace/demo/http01/demo1.go:16 +0x25

“运行时”程序包的“ NumGoroutine() ”函数返回当前正在运行的goroutine的数量。 此功能可用于调试goroutine泄漏。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
	"fmt"
	"runtime"
	"time"
)

func hello() {
	time.Sleep(15 * time.Second)
}

func main() {
	go hello()
	fmt.Println("No. of goroutines:", runtime.NumGoroutine())
}

Output
----------
No. of goroutines: 2

当输入JSON包含目标结构的字段中不存在的键时,可以使用json解码器的DisallowUnknownFields()方法返回错误。

 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
package main

import (
	"encoding/json"
	"fmt"
	"strings"
)

type person struct {
	Name string
}

func main() {
	const sample = `{"Name":"Gopher", "Age":50}`

	dec := json.NewDecoder(strings.NewReader(sample))
	dec.DisallowUnknownFields()

	var p person
	err := dec.Decode(&p)
	if err != nil {
		fmt.Println("JSON decode error:", err)
		return
	}
	fmt.Println("Name:", p.Name)
}
----------
JSON decode error: json: unknown field "Age"

Go更新

https://pbs.twimg.com/media/EhgFfxGXsAMNc2H?format=png&name=900x900

相关内容