Skip to main content

golang变量类型检测

· One min read

API

example:

package main

import (
"fmt"
"reflect"
)

type B struct {
}

func main() {
a := "123"
fmt.Println(reflect.TypeOf(a)) //string
var b B
fmt.Println(reflect.TypeOf(b)) //main.B
}

为什么要检测变量类型?

  1. 前端传过来的数据,比如说bool,int你不知道会转化成什么类型
  2. 使用别人的库,你也不知道实例是什么类型

gin拿到的都是string