O'Reilly logo

Go: Design Patterns for Real-World Projects by Mat Ryer, Mario Castro Contreras, Vladimir Vivien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Type conversion

In general, Go considers each type to be different. This means under normal circumstances, values of different types are not fungible in assignment, function parameters, and expression contexts. This is true for built-in and declared types. For instance, the following will cause a build error due to type mismatch:

package main 
import "fmt" 
 
type signal int 
 
func main() { 
   var count int32 
   var actual int 
   var test int64 = actual + count 
 
   var sig signal 
   var event int = sig 
 
   fmt.Println(test) 
   fmt.Println(event) 
} 

golang.fyi/ch04/type_conv.go

The expression actual + count causes a build time error because both variables are of different types. Even though variables actual and count are of numeric types and int32 and int have the same memory ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required