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

How to perform forced type conversion in go language

Jan 07, 2023 pm 02:05 PM
golang go language

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:

1

value, ok := x.(T)

Copy after login

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

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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)

    }

}

Copy after login

1

2

3

打印结果是:int 10

因为 golang 自动推断 a 是 int 类型。

(这个更像是Java的强制类型转换,认为变量 a 是 int 类型,就强转为 int 类型来使用)

Copy after login

2. Type conversion

1

2

3

4

5

6

7

8

9

package main

 

import "fmt"

 

func main() {

   var a float32 = 5.6

   var b int = 10

   fmt.Println (a * float32(b))

}

Copy after login

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,

1

2

3

4

var a int32  = 10

var b int64 = int64(a)

var c float32 = 12.3

var d float64 =float64(c)

Copy after login

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

1

2

3

4

5

6

7

8

package main

 

func main() {

   var a int = 10

   var p *int =&a

   var c *int64

   c= (*int64)(p)

}

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

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)

}

Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? Apr 02, 2025 pm 02:15 PM

Automatic deletion of Golang generic function type constraints in VSCode Users may encounter a strange problem when writing Golang code using VSCode. when...

See all articles