Go Cookbook
上QQ阅读APP看书,第一时间看更新

Converting data types and interface casting

Go is typically very flexible in conversion between data. A type may inherit another type as follows:

type A int

Then, we can always cast back to the type we inherited as follows:

var a A = 1
fmt.Println(int(a))

There are also convenience functions for converting between numbers with casting, between strings and other types using fmt.Sprint and with strconv, and between interfaces and types using reflection. This recipe will explore some of these basic conversions that will be used throughout the book.