当前位置:网站首页>GO语言-type关键字

GO语言-type关键字

2022-06-21 15:32:00 一边学习一边哭

type定义简单类型

语法

type关键字 类型名 type
type myint int
type mystring string

func main() {
	var i1 myint = 1
	i2 := 1
	fmt.Printf("类型比较:\ti1的类型:%T\ti2的类型:%T\n", i1, i2)

	var s1 mystring = "hello"
	s2 := "hello"
	fmt.Printf("类型比较:\ts1的类型:%T\ts2的类型:%T\n", s1, s2)
}

此时的i1 i2;s1 s2的类型是不一样的,不可以将i1 i2或s1 s2相互赋值 。

类型别名

语法

type关键字 类型别名 = type
type myint = int

func main() {
	var i1 myint = 1
	i2 := 1
	fmt.Printf("类型比较:\ti1的类型:%T\ti2的类型:%T\n", i1, i2)
	fmt.Println(i1 + i2)
}

type定义函数类型

type myfun func(int, int) string

func fun1() myfun {
	fun := func(a, b int) string {
		s := strconv.Itoa(a) + strconv.Itoa(b)
		return s
	}
	return fun
}

func main() {
	testfun := fun1()
	fmt.Println(testfun(100, 200))
}

原网站

版权声明
本文为[一边学习一边哭]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq522044637/article/details/125388072