Home >Backend Development >Golang >Two aspects introduce Golang's value conversion

Two aspects introduce Golang's value conversion

PHPz
PHPzOriginal
2023-04-03 09:18:41749browse

Type conversion is often required in Go language (Golang), especially when dealing with variables of different types, conversion between values ​​(value) is required. This article will introduce Golang's value conversion from the following two aspects:

  1. Conversion between basic types
  2. Conversion between custom types

Conversion between basic types

In Golang, conversion between basic types is very simple, you only need to use the conversion function of the corresponding type. The following is sample code for conversion between some basic types:

package main

import (
    "fmt"
)

func main() {
    var i int = 5
    var f float32 = 3.2
    var b bool = true

    // int to float32
    var iToFloat32 float32 = float32(i)
    fmt.Printf("int to float32: %v\n", iToFloat32)

    // float32 to int
    var fToInt int = int(f)
    fmt.Printf("float32 to int: %v\n", fToInt)

    // int to bool
    var iToBool bool = i != 0
    fmt.Printf("int to bool: %v\n", iToBool)

    // bool to int
    var bToInt int = 0
    if b {
        bToInt = 1
    }
    fmt.Printf("bool to int: %v\n", bToInt)
}

Conversion between custom types

In Golang, value conversion between custom types may be more troublesome because it requires Use some methods for type conversion. The following is a sample code for conversion between custom types:

package main

import (
    "fmt"
)

type MyInt int

func (m MyInt) ToString() string {
    return fmt.Sprintf("%d", m)
}

type MyFloat float32

func (m MyFloat) ToString() string {
    return fmt.Sprintf("%.2f", m)
}

func main() {
    var i MyInt = 10
    var f MyFloat = 3.14

    // MyInt to MyFloat
    var iToFloat MyFloat = MyFloat(i)
    fmt.Printf("MyInt to MyFloat: %s\n", iToFloat.ToString())

    // MyFloat to MyInt
    var fToInt MyInt = MyInt(f)
    fmt.Printf("MyFloat to MyInt: %s\n", fToInt.ToString())
}

Here, we define two custom types MyInt and MyFloat, and define a ToString() method for them respectively, which is used to convert the custom types. Define type conversion to string. In the main function, we first define a MyInt type variable i and a MyFloat type variable f, and then convert the values ​​from MyInt type to MyFloat type and from MyFloat type to MyInt type, and finally convert the converted result Convert to string type through ToString() method for easy output.

In general, value conversion in Golang is very simple. You only need to use the conversion function of the corresponding type or the conversion method of a custom type.

The above is the detailed content of Two aspects introduce Golang's value conversion. 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