SoFunction
Updated on 2025-03-04

Example of implementation of Go language basic type conversion

In Go programming, type conversion is a common operation that allows us to convert values ​​between different data types. Go provides several ways to perform type conversion to meet different programming needs. This article will introduce the basic type conversion methods in Go language in detail.

1. Conversion between string and primitive type

Go language usagestrconvPackage to implement conversion between strings and primitive types.

1.1 Convert integers to strings

useFunctions can convert integers into strings. For example:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	i := 123
	i2s := (i)
	(i2s) // Output: 123}

1.2 Convert strings to integers

useFunctions can convert strings to integers. It should be noted that this function will return two values, the second value is an error message. If the conversion fails, the error message will provide a corresponding prompt.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s := "456"
	i, err := (s)
	if err != nil {
		("Conversion error:", err)
	} else {
		(i) // Output: 456	}
}

2. Conversion between floating point numbers and strings

Go also providesFunctions to implement string to floating point conversion.

2.1 Convert strings to floating point numbers

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s := "3.14"
	f, err := (s, 64)
	if err != nil {
		("Conversion error:", err)
	} else {
		(f) // Output: 3.14	}
}

3. Conversion between boolean and string

useFunctions can convert strings to boolean types.

3.1 Convert string to boolean

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s := "true"
	b, err := (s)
	if err != nil {
		("Conversion error:", err)
	} else {
		(b) // Output: true	}
}

4. Cases between basic types

For numeric types, Go supports cast conversion.

4.1 Conversion between integers and floating point numbers

package main

import (
	"fmt"
)

func main() {
	i := 123
	f := float64(i) // Convert integers to floating point numbers	i2 := int(f)    // Convert floating point numbers to integers	(f, i2) // Output: 123.0 123}

5. Format output

Go languagefmtPackage providedFormatFloatandFormatBoolFunctions that can be used to format the output of floating point numbers and boolean values.

5.1 Format floating point numbers

package main

import (
	"fmt"
)

func main() {
	f := 3.14159
	("Format floating point number: %.2f\n", f) // Output: Formatted floating point number: 3.14}

6. Conclusion

The Go language type conversion mechanism is simple and powerful, throughstrconvPackage and casting, we can easily convert values ​​between different data types. Whether it is the conversion between strings and primitive types or the conversion between primitive types, Go provides corresponding tools and functions to support it. Mastering these conversion techniques will help you develop Go language more efficiently.

This is the end of this article about the implementation example of Go basic type conversion. For more related content on Go basic type conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!