Update 110-type-casting.md

Added some code examples. Golang roadmap looks incomplete without them.
pull/8269/head
SaintFTS 2 months ago committed by GitHub
parent 92a61e7c45
commit 8bff639056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      src/data/roadmaps/golang/content/100-go-basics/110-type-casting.md

@ -2,6 +2,43 @@
Go doesn't support automatic type conversion, but it allows type casting, which is the process of explicitly changing the variable type.
Creating variables that share same data, but with different types:
```go
var PI float32 = 3.14159
// var b int = PI // (compiler IncompatibleAssign error)
var b int = int(PI)
```
Same is true for expressions with different data types:
```go
var PI float32 = 3.14159
var times int = 32;
// c := (PI*times) // (compiler MismatchedTypes error)
c := (PI*float32(times))
```
For functions:
```go
package main
import (
. "math"
)
func main(){
var PI float32 = 3.14159
var rotations int = int(float32(32)*PI);
result := Sin(float64(rotations))) // Conversion is necessary, because math.Sin function has float64 argument
}
```
Integers and floats of different types must be casted as was shown earlier:
```go
var a int = 42
var b int32 = 8
var c int = a+int(b)
var d float64 = 24.0
var e float32 = 6.0
var f float64 = a+float64(b)
```
Visit the following resources to learn more:
- [@official@Tour of Go: Type Casting Basics](https://go.dev/tour/basics/13)

Loading…
Cancel
Save