當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Kind用法及代碼示例

GO語言"reflect"包中"Kind"類型的用法及代碼示例。

Kind 表示 Type 表示的特定類型的類型。零種類不是有效種類。

用法:

type Kind uint

const (
    Invalid Kind = iota
    Bool
    Int
    Int8
    Int16
    Int32
    Int64
    Uint
    Uint8
    Uint16
    Uint32
    Uint64
    Uintptr
    Float32
    Float64
    Complex64
    Complex128
    Array
    Chan
    Func
    Interface
    Map
    Pointer
    Slice
    String
    Struct
    UnsafePointer
)

例子:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	for _, v := range []any{"hi", 42, func() {}} {
		switch v := reflect.ValueOf(v); v.Kind() {
		case reflect.String:
			fmt.Println(v.String())
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			fmt.Println(v.Int())
		default:
			fmt.Printf("unhandled kind %s", v.Kind())
		}
	}

}

輸出:

hi
42
unhandled kind func

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Kind。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。