Go Concepts - The Type System
TODO
Deplete into Go_Language#The_Type_System and delete.
Overview
Go is statically typed. Go designers tried to alleviate some of the "heaviness" associated with statically typed languages and made it "feel" like a dynamic language. For example Go uses local type inference, which eliminates the need to specify the type unnecessarily in program, the compiler figures it out.
Go is strongly typed meaning that yes cannot be unsafely coerced into other types they're not, or at least without programmer giving explicit permission. In JavaScript, for example, implicit conversion is done based on complicated rules that are not always easy to remember.
For more details on typing, see static typing vs. dynamic typing and strong typing vs. loose typing.
Go provides a hierarchy-free type system - there are no classes and no class inheritance. It is still an object-oriented language. Go type system encourages composition, where types are composed of smaller types.
Type Definition
Zero Value
Zero value for a specific type: 0 for ints, 0.0 for floats, "" for strings, false for Booleans and nil for pointers (0x0).
For reference types, their underlying data structures are initialized to their zero values, but variable declared as a reference type set to their zero values will return a value of nil. For example, the zero value for maps, slices and channels is nil.
Built-in Types
The built-in types are the set of types that are provided by the language. The definition of the following types can be found in $GO_HOME/src/builtin/builtin.go:
bool
Integers
Floating-Point Numbers
string
Arrays
Slices
Maps
error
Function Types
A function is member of a function type. The function type is defined by its signature:
func(input_param_type1, input_param_type2, ...) (return_type1, return_type2, ...)
Example of a function that requires an int and returns an int:
func(int) int
// a variable of type func(int) int is declared var f func(int) int; // the variable is initialized with an actual function f = func(i int) int { return i + 1 }
Also see:
User-Defined Types
The user-defined types are introduced by the type keyword. There are three kinds of user-defined types: 1) structs 2) type aliasing and 3) interfaces. Behavior can be added to structs and aliased types by the way of methods.
The documentation sometimes refers to user-defined types as named types.
Structs
Type Aliasing
Interfaces
Declaring Multiple Types
type ( A struct { ... } B struct { ... } ... )
Conversion Between Types
In order to convert between types, the type name is used like a function:
var f float64 = 5.0 var i int = 5 ... result = f / float64(i)
This is equivalent with Java cast.
Note that the same syntax can be used to convert to and from user-defined types:
import "time" ... var sleepSeconds int sleepSeconds = 1 // // time.Sleep expects a time.Duration do we convert time.Second to int, multiply and convert back to time.Duration // var duration time.Duration duration = time.Duration(sleepSeconds * int(time.Second)) time.Sleep(duration)
Also see: