本教材由知了传课辛苦制作而成,仅供学习使用,请勿用于商业用途!如进行转载请务必注明出处!谢谢!

类型: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") }

1270人已阅读,今天你学习了吗?

添加新回复