search
HomeBackend DevelopmentGolangWhat is type assertion in go language?

What is type assertion in go language?

Jan 12, 2023 am 10:57 AM
golanggo languagetype assertion

In the Go language, type assertion is an operation used on interface values. It is used to check whether the value held by the interface type variable implements the expected interface or specific type. The syntax is "value, ok := x.(T)". What can be done through type assertions: 1. Check whether i is nil; 2. Check whether the value stored in i is of a certain type.

What is type assertion in go language?

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

What is an assertion?

Assertion is a programming term expressed as some Boolean expression. When writing code, we always make some assumptions, and assertions are used to capture these assumptions in the code. To simply understand assertion, it means judgment. So in Go, we can understand type assertions as type judgments.

Introduction to type assertion

Type assertion (Type Assertion) is an operation used on interface values ​​to check whether the value held by the interface type variable is implemented The desired interface or specific type.

The syntax format of type assertions in Go language 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).

This assertion expression will return the value of x (that is, value) and a Boolean value (that is, ok). You can judge whether x is of type T based on the Boolean value:

  • If T is a specific type, the type assertion will check whether the dynamic type of x is equal to the specific type T. If the check succeeds, the type assertion returns a dynamic value of x whose type is T.

  • If T is an interface type, type assertion checks whether the dynamic type of x satisfies T. If the check is successful, the dynamic value of x will not be extracted and the return value is an interface value of type T.

  • No matter what type T is, if x is a nil interface value, the type assertion will fail.

There are two main forms of type assertions in Go

  • Variable.(type). For example: i.(int)

  • variable, bool = variable.(type). For example: num,ok = i.(int). ok means to determine whether the type is successful.

Usage of type assertion

You can do the following things through type assertion

  • Check if i is nil

  • Check if i the stored value is of a certain type

There are two specific ways to use it:

The first one:

t := i.(T)

This expression can assert an interface object (i) It is not nil, and the type of the value stored in the interface object (i) is T. If the assertion succeeds, the value will be returned to t. If the assertion fails, panic will be triggered.

Let’s write a piece of code to test it

package main

import "fmt"

func main() {
    var i interface{} = 10
    t1 := i.(int)
    fmt.Println(t1)

    fmt.Println("=====分隔线=====")

    t2 := i.(string)
    fmt.Println(t2)
}

The output after running is as follows. You can find that it failed when executing the second assertion and triggered panic

10
=====分隔线=====
panic: interface conversion: interface {} is int, not string

goroutine 1 [running]:
main.main()
        E:/GoPlayer/src/main.go:12 +0x10e
exit status 2

If you want The asserted interface value is nil, so let’s see if it will trigger panic as expected

package main

func main() {
    var i interface{} // nil
    var _ = i.(interface{})
}

The output is as follows, it will indeed trigger panic

panic: interface conversion: interface is nil, not interface {}

goroutine 1 [running]:
main.main()
        E:/GoPlayer/src/main.go:5 +0x34
exit status 2

Second type

t, ok:= i.(T)

Same as above, this expression can also assert that an interface object (i) is not nil, and the type of the value stored in the interface object (i) is T. If the assertion is successful, it will be returned Its type is t, and the value of ok is true at this time, indicating that the assertion is successful.

If the type of the interface value is not the T we asserted, the assertion will fail, but unlike the first expression, this will not trigger panic, but will set the value of ok to false , indicating that the assertion failed, and t is the zero value of T at this time.

Slightly modify the above example, as follows

package main

import "fmt"

func main() {
    var i interface{} = 10
    t1, ok := i.(int)
    fmt.Printf("%d-%t\n", t1, ok)

    fmt.Println("=====分隔线1=====")

    t2, ok := i.(string)
    fmt.Printf("%s-%t\n", t2, ok)

    fmt.Println("=====分隔线2=====")

    var k interface{} // nil
    t3, ok := k.(interface{})
    fmt.Println(t3, "-", ok)

    fmt.Println("=====分隔线3=====")
    k = 10
    t4, ok := k.(interface{})
    fmt.Printf("%d-%t\n", t4, ok)

    t5, ok := k.(int)
    fmt.Printf("%d-%t\n", t5, ok)
}

The output after running is as follows. It can be found that when the second assertion was executed, although it failed, it did not trigger a panic.

10-true
=====分隔线1=====
-false
=====分隔线2=====
<nil> - false
=====分隔线3=====
10-true
10-true

In the above output, you should pay attention to that the output of the second assertion before -false is not whether there is any value of t2 output, but because the assertion failed, so What t2 gets is that the zero value of the string is also "", which is zero length, so you can't see its output.

Type Switch

If you need to distinguish between multiple types, you can use type switch assertions. This will be simpler, more direct, and more efficient than making type assertions one by one.

package main

import "fmt"

func findType(i interface{}) {
    switch x := i.(type) {
    case int:
        fmt.Println(x, "is int")
    case string:
        fmt.Println(x, "is string")
    case nil:
        fmt.Println(x, "is nil")
    default:
        fmt.Println(x, "not type matched")
    }
}

func main() {
    findType(10)      // int
    findType("hello") // string

    var k interface{} // nil
    findType(k)

    findType(10.23) //float64
}

The output is as follows

10 is int
hello is string
<nil> is nil
10.23 not type matched

Extra explanation:

  • If your value is nil, then the match is case nil
  • If your value does not match the corresponding type in switch-case, then the default branch is taken

[Related recommendations: Go video tutorial,Programming Teaching

The above is the detailed content of What is type assertion 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
Learn Go String Manipulation: Working with the 'strings' PackageLearn Go String Manipulation: Working with the 'strings' PackageMay 09, 2025 am 12:07 AM

Go's "strings" package provides rich features to make string operation efficient and simple. 1) Use strings.Contains() to check substrings. 2) strings.Split() can be used to parse data, but it should be used with caution to avoid performance problems. 3) strings.Join() is suitable for formatting strings, but for small datasets, looping = is more efficient. 4) For large strings, it is more efficient to build strings using strings.Builder.

Go: String Manipulation with the Standard 'strings' PackageGo: String Manipulation with the Standard 'strings' PackageMay 09, 2025 am 12:07 AM

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

Mastering Byte Slice Manipulation with Go's 'bytes' Package: A Practical GuideMastering Byte Slice Manipulation with Go's 'bytes' Package: A Practical GuideMay 09, 2025 am 12:02 AM

ThebytespackageinGoisessentialforefficientbyteslicemanipulation,offeringfunctionslikeContains,Index,andReplaceforsearchingandmodifyingbinarydata.Itenhancesperformanceandcodereadability,makingitavitaltoolforhandlingbinarydata,networkprotocols,andfileI

Learn Go Binary Encoding/Decoding: Working with the 'encoding/binary' PackageLearn Go Binary Encoding/Decoding: Working with the 'encoding/binary' PackageMay 08, 2025 am 12:13 AM

Go uses the "encoding/binary" package for binary encoding and decoding. 1) This package provides binary.Write and binary.Read functions for writing and reading data. 2) Pay attention to choosing the correct endian (such as BigEndian or LittleEndian). 3) Data alignment and error handling are also key to ensure the correctness and performance of the data.

Go: Byte Slice Manipulation with the Standard 'bytes' PackageGo: Byte Slice Manipulation with the Standard 'bytes' PackageMay 08, 2025 am 12:09 AM

The"bytes"packageinGooffersefficientfunctionsformanipulatingbyteslices.1)Usebytes.Joinforconcatenatingslices,2)bytes.Bufferforincrementalwriting,3)bytes.Indexorbytes.IndexByteforsearching,4)bytes.Readerforreadinginchunks,and5)bytes.SplitNor

Go encoding/binary package: Optimizing performance for binary operationsGo encoding/binary package: Optimizing performance for binary operationsMay 08, 2025 am 12:06 AM

Theencoding/binarypackageinGoiseffectiveforoptimizingbinaryoperationsduetoitssupportforendiannessandefficientdatahandling.Toenhanceperformance:1)Usebinary.NativeEndianfornativeendiannesstoavoidbyteswapping.2)BatchReadandWriteoperationstoreduceI/Oover

Go bytes package: short reference and tipsGo bytes package: short reference and tipsMay 08, 2025 am 12:05 AM

Go's bytes package is mainly used to efficiently process byte slices. 1) Using bytes.Buffer can efficiently perform string splicing to avoid unnecessary memory allocation. 2) The bytes.Equal function is used to quickly compare byte slices. 3) The bytes.Index, bytes.Split and bytes.ReplaceAll functions can be used to search and manipulate byte slices, but performance issues need to be paid attention to.

Go bytes package: practical examples for byte slice manipulationGo bytes package: practical examples for byte slice manipulationMay 08, 2025 am 12:01 AM

The byte package provides a variety of functions to efficiently process byte slices. 1) Use bytes.Contains to check the byte sequence. 2) Use bytes.Split to split byte slices. 3) Replace the byte sequence bytes.Replace. 4) Use bytes.Join to connect multiple byte slices. 5) Use bytes.Buffer to build data. 6) Combined bytes.Map for error processing and data verification.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment