Golang Is a new cross platform programming language . Today, Xiaobai will take you hand in hand into Golang The world of . ( The first 5 course )
Data type conversion can help us convert a variable of one data type into another .
Format :
type_name(expression)
Floating point to integer conversion is from high precision to low precision , It will be discarded after the decimal point .
Example :
package main
import "fmt"
func main() {
// Definition float
var num_float = 1.2
// Type conversion
var num_int = int(num_float)
// Debug output
fmt.Println(num_float)
fmt.Println(num_int)
}
Output results :
1.2
1
Example :
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
// Definition string
var str = "666"
// Type conversion
var num, _ = strconv.Atoi(str)
// Debug output
fmt.Println(str, reflect.TypeOf(str))
fmt.Println(num, reflect.TypeOf(num))
}
Output results :
666 string
666 int
Example :
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
// Definition int
var num = 999
// Type conversion
var str = strconv.Itoa(num)
// Debug output
fmt.Println(num, reflect.TypeOf(num))
fmt.Println(str, reflect.TypeOf(str))
}
Output results :
999 int
999 string