Home > Article > Backend Development > 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.
package main import ( "fmt" "strconv" ) func main() { str := "123" num, err := strconv.Atoi(str) if err != nil { fmt.Println("转换失败:", err) return } fmt.Println("转换后的整数:", num) }
Run result:
Converted integer: 123
package main import ( "fmt" "strconv" ) func main() { num := 123 str := strconv.Itoa(num) fmt.Println("转换后的字符串:", str) }
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.
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) }
Running result:
Converted floating point number: 3.14
package main import ( "fmt" "strconv" ) func main() { num := 3.14 str := strconv.FormatFloat(num, 'f', -1, 64) fmt.Println("转换后的字符串:", str) }
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.
package main import ( "fmt" ) func main() { str := "hello" slice := []byte(str) slice[0] = 'H' fmt.Println("转换后的切片:", slice) }
Run result:
Converted slice: [72 101 108 108 111]
package main import ( "fmt" ) func main() { slice := []byte{'H', 'e', 'l', 'l', 'o'} str := string(slice) fmt.Println("转换后的字符串:", str) }
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.
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) }
Run result:
Converted string: true
Conversion The resulting bool: true
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) }
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!