How to perform forced type conversion in 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))".
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 |
|
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 |
|
1 2 3 |
|
1 2 3 4 5 6 7 8 9 |
|
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 |
|
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.
1 2 3 4 5 6 7 8 |
|
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 |
|
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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? When using GoLand for Go language development, many developers will encounter custom structure tags...

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 well-known open source projects? When programming in Go, developers often encounter some common needs, ...

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

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

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