类型:type
一、类型的申明
1.9版本之前
type test int
1.9版本及之后
type test = int
示例代码
package main
import "fmt"
type test = int
func main() {
var a test
a = 1
fmt.Println(a)
fmt.Printf("%T",a)
}
二、使用
1.定义结构体
type Person struct {
Id int
}
2.定义类型别名
type new_str string
var name new_str
name = "hallen"
3.定义接口
type Animal interface {
Eat()
}
4.定义函数类型
type say func(name string) string
func Hello(name string) string {
return fmt.Sprintf("hello,%s",name)
}
func (s say) Hi(name string) string {
ret := fmt.Sprintf("say:%v",s(name))
fmt.Println(ret)
return ret
}
func main() {
say(Hello).Hi("hallen")
}