Home  >  Article  >  Backend Development  >  How to perform forced type conversion in go language

How to perform forced type conversion in go language

青灯夜游
青灯夜游Original
2023-01-07 14:05:475818browse

There are three grammatical forms of forced type conversion in the Go language: 1. Type assertion, syntax "value, ok := x. (type that needs to be converted)"; 2. Use the "type (a)" form For type conversion, the syntax is "value of type B = type B (value of type A)"; 3. Pointer type conversion, the syntax is "(*pointer type)(unsafe.Pointer(value))".

How to perform forced type conversion in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Golang is a strongly typed language and has forced type conversion, but it is different from the forced type conversion used in the Java language.

golang mandatory type conversion

The golang language is divided into type conversion (type conversion), type assertion (type assertion) and Pointer type conversion.

1. Type assertion

Type assertion (Type Assertion) is an operation used on interface values, used to check interface type variables Whether the value held implements the expected interface or concrete type.

The syntax format is as follows:

value, ok := x.(T)

Among them, x represents the type of an interface, and T represents a specific type (can also be an interface type).

Example:

package main

import "fmt"

func main() {
    var a interface{} =10
    t,ok:= a.(int)
    if ok{
        fmt.Println("int",t)
    }
    t2,ok:= a.(float32)
    if ok{
        fmt.Println("float32",t2)
    }
}
打印结果是:int 10
因为 golang 自动推断 a 是 int 类型。
(这个更像是Java的强制类型转换,认为变量 a 是 int 类型,就强转为 int 类型来使用)

2. Type conversion

package main

import "fmt"

func main() {
   var a float32 = 5.6
   var b int = 10
   fmt.Println (a * float32(b))
}

float32( in the code segment b) is the second type of forced type conversion. Common variable types int, float, and string can be forced to use type (a). For example,

var a int32  = 10
var b int64 = int64(a)
var c float32 = 12.3
var d float64 =float64(c)

This type conversion form is more like Java creates a new type object through the constructor method of the constructor class. It is not a cast in Java syntax.

3. Pointer type conversion

package main

func main() {
   var a int = 10
   var p *int =&a
   var c *int64 
   c= (*int64)(p)
}

Such code is wrong and the compiler will prompt cannot convert p (type *int) to type *int64
The forced type conversion of the pointer requires the use of the function implementation in the unsafe package

package main

import "unsafe"
import "fmt"

func main() {
    var a int =10
    var b *int =&a
    var c *int64 = (*int64)(unsafe.Pointer(b))
    fmt.Println(*c)
}

Summary

There are three grammatical forms of forced type conversion in golang, namely type assertion, type conversion, pointer type conversion, grammar The Type Assertion and Pointer Type Conversion are more similar to Java.

Golang and Java's forced type conversion can be understood comparatively, but different languages ​​have different design ideas and cannot be compared hard.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How to perform forced type conversion in go language. 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