目录
go语言中文标准库文档
Go语言标准库文档中文版 | Go语言中文网 | Golang中文社区 | Golang中国
一、一个是定义新的类型名,另一个是起个别名
func main() {
type myint int //创建一个新的类型名
type myint1 = int //起一个类型的别名
var a myint
a = 16
var b myint1
b = 17
fmt.Printf("a的类型:%T,b的类型:%T\n", a, b)//a的类型:main.myint,b的类型:int
}
a的类型:main.myint,特地说明是myint是再main包下的类型
二、在包中定义自己的方法
1、加了“=”,使得mathsqrt是bufio.Reader的别名,这时候在main包中就无法在定义属于bufio包中的方法。
package main
import "bufio"
// type类型关键字
func main() {
}
type mathsqprt = bufio.Reader
func (T mathsqprt) add() { // cannot define new methods on non-local type bufio.Reader
}
2、如果一定想要在main包中把自己的方法定义到bufio.Reader中,我们可以不用“=”,就相当于定义了一个新的类型名,实质上就是把bufio.Reader拷贝一份到mathsqrt中,再在mathsqrt中创建属于main下的bufio.Reader的方法
package main
import "bufio"
// type类型关键字
func main() {
}
type mathsqprt bufio.Reader
func (T mathsqprt) add() {
}
总结:无等号就是创建新的类型名(复制一份到新的类型中)
有等号就是起个别名,就是一样的东西