golang 官方FAQ

起源

项目的目的

  • 充分使用多核,并发安全
  • 垃圾回收,简单易用
  • 快速编译
  • 来源于实践,解决google软件开发中的低效问题
    Clear dependencies
    Clear syntax
    Clear semantics
    Composition over inheritance
    Simplicity provided by the programming model (garbage collection, concurrency)
    Easy tooling (the go tool, gofmt, godoc, gofix)

历史

Robert Griesemer, Rob Pike and Ken Thompson 2007
Russ Cox joined in late 2008
Go became a public open source project on November 10, 2009

设计原则

We felt it should be possible to have the efficiency, the safety, and the fluidity in a single language.

说的是一回事啊

设计

runtime

go没有vm,直接编译成机器码,它的runtime类似于libc,提供了垃圾回收等后台线程。

instance of判断类型,
type T struct{}
var _ I = T{} // Verify that T implements I.
var _ I = (*T)(nil) // Verify that *T implements I.
这个在pb或者其他自动生成的代码里面很常见
为了避免别人定义的数据结构错误的满足自己的interface,可以在自己的interface里面定义一个很长的方法
If you wish the users of an interface to explicitly declare that they implement it, you can add a method with a descriptive name to the interface’s method set. For example:

type Fooer interface {
Foo()
ImplementsFooer()
}
A type must then implement the ImplementsFooer method to be a Fooer, clearly documenting the fact and announcing it in go doc’s output.

type Bar struct{}
func (b Bar) ImplementsFooer() {}
func (b Bar) Foo() {}
Most code doesn’t make use of such constraints, since they limit the utility of the interface idea. Sometimes, though, they’re necessary to resolve ambiguities among similar interfaces.

nil错误
func returnsError() error {
var p *MyError = nil
if bad() {
p = ErrBad
}
return p // Will always return a non-nil error.
}

An interface value is nil only if the V and T are both unset, (T=nil, V is not set),

golang的const是没有类型的,只有在使用的时候才会获得具体的类型。

给interface赋值的时候其实会复制

interface不可以取地址,也不存在指向interface的指针

对于是使用pointer还是value作为方法,除了修改原数据,效率意外,还必须保持一致性,如果有的是指针,那么所有方法都要是指针

T和*T方法集合

As the Go specification says, the method set of a type T consists of all methods with receiver type T, while that of the corresponding pointer type *T consists of all methods with receiver *T or T. That means the method set of *T includes that of T, but not the reverse.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值