Skip to content

[Bug-语法进阶-反射-Kind]:String()方法的返回值来自于一个其内部的切片,不是 map #54

@hengzhang941235

Description

@hengzhang941235

环境

网站地址(必填):
https://golang.xiniushu.com/%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8/%E8%AF%AD%E6%B3%95%E8%BF%9B%E9%98%B6/105.reflect.html#kind
https://golang.halfiisland.com/essential/senior/105.reflect.html#kind

章节目录(必填): 语法进阶-反射-Kind

问题描述

原文:Kind类型仅仅实现了Stringer接口的String()方法,该类型也仅有这一个方法,String()方法的返回值来自于一个其内部的map,如下所示

问题:kindNames 不是 map,它是一个 slice(切片),定义如下:
var kindNames = []string{ ... }
这种写法乍一看很像 map,但其实这是 Go 中的一种特殊的写法:索引初始化语法(index expressions in slice literals)。

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions