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

How to determine data type in golang

silencement
Release: 2019-12-25 10:32:11
Original
7512 people have browsed it

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}
Copy after login

Use reflection:
reflect.TypeOf(x)

package main

import (
    "fmt"
    "reflect"
)

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

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template