Tutorial by Examples

There are two basic styles of type conversion in Go: // Simple type conversion var x := Foo{} // x is of type Foo var y := (Bar)Foo // y is of type Bar, unless Foo cannot be cast to Bar, then compile-time error occurs. // Extended type conversion var z,ok := x.(Bar) // z is of type Bar, o...
As Go uses implicit interface implementation, you will not get a compile-time error if your struct does not implement an interface you had intended to implement. You can test the implementation explicitly using type casting: type MyInterface interface { Thing() } type MyImplementer struct {} ...
This example illustrates how Go's type system can be used to implement some unit system. package main import ( "fmt" ) type MetersPerSecond float64 type KilometersPerHour float64 func (mps MetersPerSecond) toKilometersPerHour() KilometersPerHour { return KilometersPer...

Page 1 of 1