go 中的文章

小白的Go语言编程索引

基础 The Go Programming Language Specification 基本数据类型 testing testify - 第三方断言库 goconvey - BDD库 类型别名 IDE vscode-go……

阅读全文

Go 1.8的几个新特性

Go1.8发布了,这里先简单学习一下Go1.8的几个新特性。 新的切片排序Api 在以前要对切片排序要实现sort.Interface接口: 1package main 2 3import ( 4 "fmt" 5 "sort" 6) 7 8type Person struct { 9 Name string 10 Age int 11} 12 13func (p Person) String() string { 14 return fmt.Sprintf("%s: %d", p.Name, p.Age) 15} 16 17// ByAge implements sort.Interface for []Person based on 18// the Age field. 19type ByAge []Person……

阅读全文

Go语言中关于JSON的整理

JSON作为一种在不同平台间的数据交换格式,Go的标准包encoding/json中实现了对json的编码(marshal)和解码(unmarshal)功能。 json.Marshal json.Marshal函数实现Json的编码: 如果给定的值不等于nil并且实现……

阅读全文

Golang 1.7 GC的简单理解

从Go 1.7 runtime包理解Golang GC Go也是垃圾回收的,实现方式和别的语言不太一样。 先从Go的标准库的runtime包说起,这个包有很多运行时相关的类型和函数。 调用runtimea.GC()可以触发GC,但我们一般不会这么做,先读一下……

阅读全文

从源码理解Go:map

map的内部结构 Go中的map使用哈希表实现的,在源码go runtime/hashmap.go中可以看到对应实现。 1// A header for a Go map. 2type hmap struct { 3 // Note: the format of the Hmap is encoded in ../../cmd/internal/gc/reflect.go and 4 // ../reflect/type.go. Don't change this structure without also changing that code! 5 count int // # live cells == size of map. Must be first (used by len() builtin) 6 flags uint8 7 B uint8 // log_2 of……

阅读全文

从源码理解Go:slice

slice的内部结构 一个slice本质上是一个数组的某个部分的引用。在go runtime/slice.go源码中定义了slice的结构: 1type slice struct { 2 array unsafe.Pointer 3 len int 4 cap int 5} 可以看到在Go的内部,slice是一个包含3个字段的结构体: array: 指向slice……

阅读全文

Golang 笔记:临时对象池sync.Pool

sync.Pool可以被看做是存放可被重用的值的容器,这个容器具有以下特性:可自动伸缩、高效、并发安全。 因为它的使用场景并不适用于诸如数据库连接池这类需要我们自己管理生命周期的资源对象的池化场景,所以一般把sync.Pool称为临时对象池(……

阅读全文

Golang 笔记:channel

CSP(CommunicatingSequentialProcess)中文翻译"通信顺序进程"或"交换信息的循序进程", CSP描述了一种并发系统进行交互的模式。 CSP允许使用进程组件来描述系统,这……

阅读全文