Home >Backend Development >Golang >How to determine data type in golang

How to determine data type in golang

silencement
silencementOriginal
2019-12-25 10:32:117501browse

How to determine data type in golang

Use Go's empty interface:
i.(type) can only be used in switch, the function has no return value

func m_type(i interface{}) {    switch i.(type) {
    case string:
        //...
    case int:
        //...
    }    return}

Use reflection:
reflect.TypeOf(x)

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var x int32 = 20
    fmt.Println("type:", reflect.TypeOf(x))
}

Summary: The first method requires knowing how many types there are, and the second method can be used for any object.

Recommended to study "golang tutorial"

The above is the detailed content of How to determine data type in golang. 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