Home  >  Article  >  Backend Development  >  A complete list of data conversion methods in Golang

A complete list of data conversion methods in Golang

WBOY
WBOYOriginal
2023-12-23 11:54:571317browse

A complete list of data conversion methods in Golang

Comprehensive list of data conversion methods in Golang, specific code examples are required

In Golang, data conversion is one of the problems we often need to deal with. Converting between different data types can help us achieve various functions and needs. This article will introduce you to some commonly used data conversion methods in Golang and give specific code examples.

  1. Conversion between strings and integers

Conversion between strings and integers is one of the most common data conversions. In Golang, we can use the strconv package to achieve this conversion.

The sample code is as follows:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "123"
    num, err := strconv.Atoi(str) // 字符串转整数
    if err != nil {
        fmt.Println("转换失败:", err)
    } else {
        fmt.Println("转换结果:", num)
    }

    num2 := 456
    str2 := strconv.Itoa(num2) // 整数转字符串
    fmt.Println("转换结果:", str2)
}
  1. Conversion between strings and floating point numbers

Conversion between strings and floating point numbers is also common One of the data conversion tasks. In Golang, we can also use the strconv package to achieve this conversion.

The sample code is as follows:

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)
    }

    num2 := 2.718
    str2 := strconv.FormatFloat(num2, 'f', 2, 64) // 浮点数转字符串
    fmt.Println("转换结果:", str2)
}
  1. Conversion between strings and Boolean values

The conversion between strings and Boolean values ​​is also what we are doing Common requirements in Golang. In Golang, we can use the ParseBool and FormatBool functions of the strconv package to achieve this conversion.

The sample code is as follows:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "true"
    b, err := strconv.ParseBool(str) // 字符串转布尔值
    if err != nil {
        fmt.Println("转换失败:", err)
    } else {
        fmt.Println("转换结果:", b)
    }

    b2 := false
    str2 := strconv.FormatBool(b2) // 布尔值转字符串
    fmt.Println("转换结果:", str2)
}
  1. Conversion between strings and byte slices

In Golang, between strings and byte slices Conversion between can be achieved using the bytes package and strings package in the standard library.

The sample code is as follows:

package main

import (
    "fmt"
    "bytes"
)

func main() {
    str := "hello"
    b := []byte(str) // 字符串转字节切片
    fmt.Println("转换结果:", b)

    str2 := string(b) // 字节切片转字符串
    fmt.Println("转换结果:", str2)
}
  1. Conversion between other data types

In addition to the conversion between the above common data types, Golang also provides It also provides conversion methods for other data types, such as conversion between integers, conversion between floating point numbers, etc.

The sample code is as follows:

package main

import (
    "fmt"
)

func main() {
    // 整数之间的转换
    num := 123
    num2 := int32(num)
    fmt.Println("转换结果:", num2)

    // 浮点数之间的转换
    num3 := 3.14
    num4 := float32(num3)
    fmt.Println("转换结果:", num4)
}

The above are some common data conversion methods in Golang and the corresponding specific code examples. Through these methods, we can easily convert between different data types to meet our various needs. I hope this article can be helpful to your data conversion work in Golang.

The above is the detailed content of A complete list of data 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