Skip to content

Instantly share code, notes, and snippets.

@JeOam
Created September 21, 2016 02:21
Show Gist options
  • Select an option

  • Save JeOam/1e338afed67c34dd60f8602a40ca866f to your computer and use it in GitHub Desktop.

Select an option

Save JeOam/1e338afed67c34dd60f8602a40ca866f to your computer and use it in GitHub Desktop.
Go 入门笔记
@JeOam

JeOam commented Sep 23, 2016

Copy link
Copy Markdown
Author

new()make() 的区别:

  • new(T) 为每个新的类型T分配一片内存,初始化为 0 并且返回类型为*T的内存地址:这种方法 返回一个指向类型为 T,值为 0 的地址的指针,它适用于值类型如数组和结构体;它相当于 &T{}
  • make(T) 返回一个类型为 T 的初始值,它只适用于3种内建的引用类型:切片、map 和 channel。

换言之,new 函数分配内存,make 函数初始化;

@JeOam

JeOam commented Sep 24, 2016

Copy link
Copy Markdown
Author

有个被称作 Communicating Sequential Processes(顺序通信处理)(CSP, C. Hoare 发明的)还有一个叫做 message passing-model(消息传递)(已经运用在了其他语言中,比如 Erlang)。

@JeOam

JeOam commented Sep 26, 2016

Copy link
Copy Markdown
Author

What does an underscore in front of an import statement mean in Golang?

import _ "net/http/pprof"

This form of import makes clear that the package is being imported for its side effects, because there is no other possible use of the package: in this file, it doesn't have a name.

Doc: https://golang.org/doc/effective_go.html#blank_import

@JeOam

JeOam commented Sep 28, 2016

Copy link
Copy Markdown
Author

代码格式化当前目录所有 go 文件:

go fmt ./...

@JeOam

JeOam commented Oct 12, 2016

Copy link
Copy Markdown
Author

类型断言:

if v, ok := varI.(T); ok {  // checked type assertion
    Process(v)
    return
}
// varI is not of type T

如果转换合法,v 是 varI 转换到类型 T 的值,ok 会是 true;否则 v 是类型 T 的零值,ok 是 false,也没有运行时错误发生。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment