Home > Backend Development > Golang > Golang data conversion skills and examples sharing

Golang data conversion skills and examples sharing

PHPz
Release: 2023-12-23 10:01:18
Original
966 people have browsed it

Golang data conversion skills and examples sharing

Golang’s data conversion skills and examples are shared

Introduction:
Golang is a powerful programming language that handles various data types and format conversions It provides many convenient functions and techniques. This article will share some practical tips for data conversion in Golang and provide specific code examples.

1. Conversion between strings and integers
In Golang, conversion between strings and integers is a frequently encountered requirement. Golang provides the strconv package, whose Atoi() and Itoa() functions can easily convert strings and integers to and from each other.

  1. Convert string to integer
    Sample code:
package main

import (
    "fmt"
    "strconv"
)

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

Run result:
Converted integer: 123

  1. Integer to string
    Sample code:
package main

import (
    "fmt"
    "strconv"
)

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

Running result:
Converted string: 123

2. Conversion between strings and floating point numbers
In addition to conversion between strings and integers, Golang also provides conversion functions between strings and floating point numbers. This is a very useful function in price calculations or other scenarios that require precise calculations.

  1. Convert string to floating point number
    Sample code:
package main

import (
    "fmt"
    "strconv"
)

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

Running result:
Converted floating point number: 3.14

  1. Floating point number to string
    Sample code:
package main

import (
    "fmt"
    "strconv"
)

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

Running result:
Converted string: 3.14

3. Between slices and strings Conversion
Conversion between slices and strings is also one of the common operations in Golang. We can convert a string into a slice and modify it; or convert a slice into a string as output or storage content.

  1. Convert string to slice
    Sample code:
package main

import (
    "fmt"
)

func main() {
    str := "hello"
    slice := []byte(str)
    slice[0] = 'H'
    fmt.Println("转换后的切片:", slice)
}
Copy after login

Run result:
Converted slice: [72 101 108 108 111]

  1. Slice to string
    Sample code:
package main

import (
    "fmt"
)

func main() {
    slice := []byte{'H', 'e', 'l', 'l', 'o'}
    str := string(slice)
    fmt.Println("转换后的字符串:", str)
}
Copy after login

Run result:
Converted string: Hello

IV. Other data types Conversion between
In addition to conversion between strings, integers and floating point numbers, Golang also provides conversion functions between some other data types, such as conversion between bool and string, time and string conversion, etc.

  1. Conversion between bool and string
    Sample code:
package main

import (
    "fmt"
    "strconv"
)

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

    str2 := "true"
    bool, err := strconv.ParseBool(str2)
    if err != nil {
        fmt.Println("转换失败:", err)
        return
    }
    fmt.Println("转换后的bool:", bool)
}
Copy after login

Run result:
Converted string: true
Conversion The resulting bool: true

  1. Conversion between time and string
    Sample code:
package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    str := now.Format("2006-01-02 15:04:05")
    fmt.Println("转换后的字符串:", str)

    str2 := "2022-01-01 12:00:00"
    time, err := time.Parse("2006-01-02 15:04:05", str2)
    if err != nil {
        fmt.Println("转换失败:", err)
        return
    }
    fmt.Println("转换后的时间:", time)
}
Copy after login

Run result:
Converted string: 2022-06-20 09:54:36
Time after conversion: 2022-01-01 12:00:00 0000 UTC

Summary:
This article introduces the method of data conversion in Golang Some practical tips and concrete code examples are provided. Through these techniques, we can easily convert between different data types and improve the flexibility and reusability of the code. I hope readers can use these techniques in actual development to improve work efficiency.

The above is the detailed content of Golang data conversion skills and examples sharing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template