Skip to content

Type conversions

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.

Convert between different numeric types and between string and byte slice.

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)
}
Terminal window
42 42 42
[104 101 108 108 111]
hello