Go language (Golang) is an open source programming language influenced by languages such as C, Java, and Python. It was developed by Google to create efficient and reliable software. A program is closely related to the language in which it is written. Like the language that expresses emotions and thoughts, programming languages also have their own unique characteristics. Type conversion is a very important concept in the Go language, because type conversion is used in many situations. This article will introduce the relevant knowledge of Golang type conversion.
1. Overview of type conversion
Type conversion is the process of converting a value of one data type into a value of another data type. In Go language, data types are divided into basic data types and composite types. Basic data types include int, float, string, bool and other types. Composite types include arrays, slices, dictionaries, structures, interfaces, and functions. In the Go language, values between different types cannot be directly operated and compared. Therefore, it is necessary to type-convert values between different types. Golang supports conversion of signed integers, unsigned integers, floating point numbers, Boolean, strings and other types. The syntax for type conversion is: T(x), where T represents the type to be converted and x represents the value to be converted.
2. Basic data type conversion
1. Integer type conversion
In Go language, integer types include signed integers and unsigned integers. The integer types that support conversion are int8, int16, int32, int64, uint8, uint16, uint32 and uint64. Among them, int8 and uint8 are called byte types, int16 and uint16 are called short integer types, int32 and uint32 are called long integer types, and int64 and uint64 are called long integer types.
You need to pay attention to the following two points when converting integer types:
The following are some examples of integer type conversion:
package main import "fmt" func main() { var a int32 = 100 var b int64 = int64(a) // int32转换成int64 var c int8 = int8(a) // int32转换成int8,可能溢出 var d uint16 = uint16(a) // int32转换成uint16 fmt.Println(b, c, d) }
The output result is:
100 100 100
2. Floating point type conversion
In Go In the language, floating-point number types include float32 and float64, and the only floating-point number types that support conversion are float32 and float64. There are two points to note when converting floating-point number types:
The following is an example of floating point type conversion:
package main import "fmt" func main() { var a float32 = 3.1415926 var b float64 = float64(a) // float32转换成float64 fmt.Println(b) }
The output result is:
3.1415927410125732
3. Boolean type conversion
In Go In the language, the Boolean type has only two values: true and false, and the only types that support conversion are int and string types. When converting a Boolean value to an int, true is converted to 1 and false is converted to 0. When converting a Boolean value to a string, true is converted to "true" and false is converted to "false".
The following is an example of Boolean type conversion:
package main import "fmt" func main() { var a bool = true var b int = int(a) // true转换成int,值为1 var c string = string(a) // true转换成字符串,值为"true" fmt.Println(b, c) }
The output result is:
1 true
4. String type conversion
In Go language, A string is an (immutable) array composed of character sequences, and the only types that support conversion are primitive types. String conversion can be achieved through the strconv package. When converting integers to strings, you can use the strconv.Itoa() function, and when converting floating-point numbers to strings, you can use the strconv.FormatFloat() function.
The following is an example of string type conversion:
package main import ( "fmt" "strconv" ) func main() { var a int = 100 var b string = strconv.Itoa(a) // 将整数转换成字符串 var c float64 = 3.1415926 var d string = strconv.FormatFloat(c, 'f', 6, 64) // 将浮点数转换成字符串,保留6位小数 fmt.Println(b, d) }
The output result is:
100 3.141593
3. Composite type conversion
1. Array type conversion
In the Go language, an array is a fixed-length sequence composed of elements of the same type. Values can be assigned directly between arrays, but it should be noted that the size and type of the arrays must be the same, otherwise a compilation error will occur.
The following is an example of array type conversion:
package main import "fmt" func main() { var a [3]int = [3]int{1, 2, 3} var b [3]int = a // 数组之间可以直接赋值 fmt.Println(b) }
The output result is:
[1 2 3]
2. Slice type conversion
In Go language, slice It is a structure that contains a pointer to an array, the length and capacity of the slice, and is a variable-length sequence. Values can be assigned directly between slices, but it should be noted that the element types of the slices must be the same.
The following is an example of slice type conversion:
package main import "fmt" func main() { a := []int{1, 2, 3, 4} b := a // 切片之间可以直接赋值 fmt.Println(b) }
The output result is:
[1 2 3 4]
3. Dictionary type conversion
In Go language, dictionary Is a collection of key-value pairs. Values can be assigned directly between dictionaries, but it should be noted that the key and value types of the dictionaries must be the same.
The following is an example of dictionary type conversion:
package main import "fmt" func main() { a := map[string]int{"apple": 1, "banana": 2} b := a // 字典之间可以直接赋值 fmt.Println(b) }
The output result is:
map[apple:1 banana:2]
4. Summary
In the Go language, type conversion is a very important concept. Type conversion can convert values between different types to meet the needs of the program. However, it should be noted that when performing type conversion, it is necessary to avoid data type range overflow and precision loss problems, and at the same time, it is necessary to ensure that the type after conversion is compatible with the type before conversion. Type conversion is applied between basic data types and composite data types. Mastering the relevant knowledge of type conversion is very important for Golang programming.
The above is the detailed content of golang type conversion ok. For more information, please follow other related articles on the PHP Chinese website!