Go-Type
来源:互联网 发布:在c语言中随机数函数是 编辑:程序博客网 时间:2024/06/05 10:47
在谈论struct和interface已经用到了type这个关键字。
另外,Go的type另外一种常用功能,是类似于C/C++的typedef。在Go的package中,这种用法非常常见。
A type declaration defines a new named type that has the same underlying type as an existing type. The named type provides a way to separate different and perhaps incompatible uses of the underlying type so that they can’t be mixed unintentionally.
type name underlying-type
示例:
package main import ( "fmt")type Hello interface { out()}type MyInt int func (myInt *MyInt) out() { fmt.Println("MyInt.out():", *myInt)}func foo(i MyInt) { fmt.Println("foo():", i)}func main() { var i MyInt i = 5 i.out() //MyInt.out(): 5 foo(i) // foo(): 5 foo(1) // foo(): 1 var x int x = 100 //foo(x) //cannot use x (type int) as type MyInt in argument to foo foo(MyInt(x)) // foo(): 100}
注:foo(MyInt(x)) 给出了Go中强制类型转换的使用示例。——和C/C++的类型转换语法刚好相反(e.g. foo((MyInt)x))。
0 0
- Go-Type
- go interface type
- go Type Identical &Value Assignability & Type Conversions
- 《Go in Action 2015.11.pdf》之Go’s type system
- go语言type使用小技巧
- 一人千面:谈谈Go语言中的type
- Go语言中的type含义(转载)
- Go 1.9 新特性 Type Alias 详解
- go语言的类型断言(Type Assertion)
- Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法
- go 语言type Method Values vs. Method Expressions
- How to serialize/unserialize type struct in GO
- Go语言interface的value.(type)使用小技巧
- go语言的type的比较和赋值
- go语言中type的几种使用
- go语言中type的几种使用
- Go!
- GO
- 第3天新手SEO基础计划之:设置域名
- 建议81:使用Parallel简化同步状态下Task的使用
- ActiveMQ入门实例
- ruby奇技淫巧 之 利用Fixnum的upto和downto方法来遍历数组
- SQL创建TABLE
- Go-Type
- Notification及BroadcastReceiver
- cd
- Docker为整个软件生命周期提供安全保障
- 关于NIO的博文推荐
- linux zip命令
- DP与SP的区别
- flash从外部引入图片
- XML文件的读写