Home  >  Article  >  Backend Development  >  Explore commonly used data type conversion methods in Golang

Explore commonly used data type conversion methods in Golang

PHPz
PHPzOriginal
2023-12-23 11:26:251012browse

Explore commonly used data type conversion methods in Golang

Exploring commonly used data type conversion methods in Golang

  1. Preface

In Golang programming, you often encounter different Conversion issues between data types. This article will introduce some commonly used data type conversion methods, and attach specific code examples for your reference.

  1. Conversion between strings and integers

In Golang, conversion between strings and integers is a relatively common operation. The following is a sample code for converting a string to an integer:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "123"
    num, err := strconv.Atoi(str)
    if err != nil {
        fmt.Println("转换失败:", err)
    } else {
        fmt.Println("转换后的整数为:", num)
    }
}

The above code uses the strconv.Atoi() function to convert a string to an integer. If the conversion is successful, the corresponding integer value is returned; if the conversion fails, an error message is returned.

Converting integers to strings is also very simple and can be achieved using the strconv.Itoa() function. The following is a sample code for converting an integer to a string:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    num := 123
    str := strconv.Itoa(num)
    fmt.Println("转换后的字符串为:", str)
}

In the above code, use the strconv.Itoa() function to convert an integer to a string.

  1. Conversion between strings and floating-point numbers

In Golang, conversion between strings and floating-point numbers is also a common requirement. The following is a sample code for converting a string to a floating point number:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "3.14"
    num, err := strconv.ParseFloat(str, 64)
    if err != nil {
        fmt.Println("转换失败:", err)
    } else {
        fmt.Println("转换后的浮点数为:", num)
    }
}

The above code uses the strconv.ParseFloat() function to convert a string to a floating point number. The second parameter specifies the precision of the floating point number, and the specific value can be adjusted as needed.

Converting floating point numbers to strings is also very simple and can be achieved using the strconv.FormatFloat() function. The following is a sample code for converting floating point numbers to strings:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    num := 3.14
    str := strconv.FormatFloat(num, 'f', 2, 64)
    fmt.Println("转换后的字符串为:", str)
}

In the above code, use the strconv.FormatFloat() function to convert floating point numbers to strings. The second parameter specifies the format of the floating point number, 'f' represents a floating point number, the third parameter specifies the number of decimal places retained, and the fourth parameter specifies the precision of the floating point number.

  1. Conversion between strings and Boolean values

In Golang, conversion between strings and Boolean values ​​is also a common operation. The following is a sample code for converting a string to a Boolean value:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "true"
    b, err := strconv.ParseBool(str)
    if err != nil {
        fmt.Println("转换失败:", err)
    } else {
        fmt.Println("转换后的布尔值为:", b)
    }
}

The above code uses the strconv.ParseBool() function to convert a string to a Boolean value. If the string is "true", "1", "t", "T", "yes", "y", or "on", it is converted to true; if it is other values, it is converted is false.

Converting a Boolean value to a string is also very simple and can be achieved using the strconv.FormatBool() function. The following is a sample code for converting a Boolean value to a string:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b := true
    str := strconv.FormatBool(b)
    fmt.Println("转换后的字符串为:", str)
}

In the above code, use the strconv.FormatBool() function to convert a Boolean value to a string.

  1. Summary

This article introduces commonly used data type conversion methods in Golang, including strings and integers, strings and floating point numbers, strings and Boolean values mutual conversion. We hope that these sample codes can help readers better understand and master data type conversion techniques. If you have any questions or suggestions, please feel free to leave a message to communicate.

The above is the detailed content of Explore commonly used data type conversion methods in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn