golang is a strongly typed language. Although this writing method is often seen in code, i:=10 is actually the compiler automatically makes type inference during compilation. The compiler performs type checks on the data. Different types of data cannot be assigned values and parameters cannot be passed in functions. Strongly typed languages have some advantages. Many errors will be checked during compilation. I don’t want weak-type languages such as php and python. Many errors can only be discovered if they are run. Similarly, strong typing also has some disadvantages. When writing code, you have to consider the data type and lose some flexibility.
Get back to the point, start golang's type conversion problem
There is a little difference between golang's type conversion and C/C++ Java and other languages
- Languages such as C/C++ have implicit type conversions, but not in golang.
- Type conversion in golang is divided into cast type conversion and type assertion
In C/C++
int main() { int a=5; float b=3.5; printf("%f",a*b); }
There is no problem with such code, the compiler implicitly converts a to float type.
But in golang
package main import "fmt" func main() { var a float32 = 5.6 var b int = 10 (a * b) }
Such code will report an error because the type does not match
This is when a type conversion is required
package main import "fmt" func main() { var a float32 = 5.6 var b int = 10 (a * float32(b)) }
This way there will be no error
Normal variable typeint,float,string
You can use type (a) to perform casting, for example
var a int32 = 10 var b int64 = int64(a) var c float32 = 12.3 var d float64 =float64(c)
There are also types of pointers in golang.
package main func main() { var a int = 10 var p *int =&a var c *int64 c= (*int64)(p) }
Such code is wrong, the compiler will prompt cannot convert p (type *int) to type *int64
Pointer casting requires the function implementation in the unsafe package
package main import "unsafe" import "fmt" func main() { var a int =10 var b *int =&a var c *int64 = (*int64)((b)) (*c) }
There is also a type judgment in golang, type assertion
package main import "fmt" func main() { var a interface{} =10 switch a.(type){ case int: ("int") case float32: ("string") } }
The output result of the program is int
There is another usage of type assertions
package main import "fmt" func main() { var a interface{} =10 t,ok:= a.(int) if ok{ ("int",t) } t2,ok:= a.(float32) if ok{ ("float32",t2) } }
t,ok:= a.(int) has two return values. The first is the value of the corresponding type and the second is the bool type. The type judgment is correct.
Okay, golang's coercive type conversion has been written here. I was sleepy in the middle of the night, so I'm going to add something to my mind. Next time I write about the conversion between numbers and strings in golang
This is the end of this article about the implementation of golang's cast type conversion. For more related golang's cast type conversion content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!