Type conversions
Context
Section titled “Context”Go does not have implicit type conversions. You must explicitly convert values using T(v), where T is the target type and v is the value.
Example
Section titled “Example”Convert between different numeric types and between string and byte slice.
Code example
Section titled “Code example”package main
import "fmt"
func main() { var a int = 42 var b float64 = float64(a) var c uint = uint(a)
fmt.Println(a, b, c)
// String to byte slice s := "hello" bs := []byte(s) fmt.Println(bs)
// Byte slice to string s2 := string(bs) fmt.Println(s2)}Output
Section titled “Output”42 42 42[104 101 108 108 111]hello