go语言特性1

function and method

A method is just a function with a receiver argument.

func split(sum int) (x, y int)
#Go's return values may be named. If so, they are treated as variables defined at the top of the function.
#A return statement without arguments returns the named return values.

method sets

每一种类型都有自己的方法集method set.
value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers.
#类型T的方法集合:所有receiver类型为T的方法
The method set of a defined type T consists of all methods declared with receiver type T.
#指向类型T的指针的方法集合:所有receiver为T或*T的方法
The method set of a pointer to a defined type T (where T is neither a pointer nor an interface) is the set of all
methods declared with receiver *T or T.

#如果M是T类型的一个方法,T.M的结果就是一个函数,函数的第一个参数是T类型的变量
If M is in the method set of type T, T.M is a function that is callable as a regular function with the same arguments
as M prefixed by an additional argument that is the receiver of the method.

struct

The size of a zero-field struct type is zero.
#如果使用了json tag,就同时把字段的首字母大写,否则json也是解析不了的
The functions in the encoding/json standard package will only encode and decode the exported struct fields.

The last "," in a composite literal is optional if the last item in the literal and the closing } are at the
same line.
Otherwise, the last "," is required.

var _ = Book {
	author: "Tapir",
	pages: 256,
	title: "Go 101",#here, the "," must be present
}
#The last "," in the following line is optional.
var _ = Book{author: "Tapir", pages: 256, title: "Go 101",}

interface

An interface type defines a type set.
#type set是实现了该接口的所有类型的集合.
Every type that is a member of the type set of an interface implements that interface.

#接口中的元素是method或者type(可以是int,另一个自定义的interface类型等)
#基本接口(basic interface),接口中的元素都是方法
#T类型的方法集中包含了接口中的所有方法,就是T类型实现了该接口
The type set defined by such an interface is the set of types which implement all of those methods, and the
corresponding method set consists exactly of the methods specified by the interface.

#所有的类型都实现了空接口interface{}
All types implement the empty interface which stands for the set of all (non-interface) types.

#别名
type byte = uint8
type rune = int32
type any = interface{}

channel

channel是协程通信的方式
chan type_name#传输type_name类型数据的管道
chan struct{}#表示不关心传输的数据是什么,只关心有数据传输了,作为一种信号机制

<-chan type_name#只读的管道
chan<- type_name#只写的管道

make和new

new
#返回某个类型的指针,为该类型分配内存,该类型的数据被置0
#new(T) allocates zeroed storage for a new item of type T and returns its address, a value of type *T.
make
#返回初始化后的值,仅用于切片,map,管道,不返回指针.
#Remember that make applies only to maps, slices and channels and does not return a pointer.
#It creates slices, maps, and channels only, and it returns an initialized (not zeroed) value of type T (not *T).
#The reason for the distinction is that these three types represent, under the covers, references to data structures
#that must be initialized before use.

#allocates an array of 100 ints and then creates a slice structure with length 10 and a capacity of 100
#pointing at the first 10 elements of the array.
#不指定cap(第三个参数),则cap的值和len的值一样
make([]int, 10, 100)

shorthand

#如果&x的方法集中包含m方法
If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m().
#结构体指针访问结构体中成员可以直接通过.运算符
If the type of x is a defined pointer type and (*x).f is a valid selector expression denoting a
field (but not a method), x.f is shorthand for (*x).f.
#如果a是指向数组的指针
For a of pointer to array type:a[x] is shorthand for (*a)[x].
If a is a pointer to an array, a[low : high] is shorthand for (*a)[low : high].

变量

//变量声明
var i int
var i int = 2
var i = 42
j := 3//自动推断类型,shorthand(var j = 3)
i, j := 0, 10
ch := make(chan int)
//通过这种形式定义的变量,可以重复声明
//Redeclaration does not introduce a new variable; it just assigns a new value to the original.

//The blank identifier(下划线) provides a way to ignore right-hand side values in an assignment:
_ = x// evaluate x but ignore it
x, _ = f()// evaluate f() but ignore second result value

https://go.dev/ref/spec
https://go.dev/ref/mod
https://go.dev/doc/effective_go
https://stackoverflow.com/questions/59349879/whats-the-difference-between-int-and-int-in-go
https://go101.org/article/interface.html
https://go101.org/article/struct.html
https://go.dev/tour/list

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值