Go 使用iota时,我感到困惑 您使用的围棋版本是什么? 此版本是否与最新版本一起复制?

Go 使用iota时,我感到困惑 您使用的围棋版本是什么? 此版本是否与最新版本一起复制?,go,Go,对 您使用的是什么操作系统和处理器体系结构? go环境输出 你做了什么? 结果: 你期望看到什么? 你看到了什么? 状态为状态类型。这与字段T.State是否是结构的一部分无关。类型状态的零值为0,这也等于Running。因此,当您将t赋值给t.State未初始化的t实例时,t.State会得到零值,该值正在运行 从代码中我可以看出,State似乎有一个“Unknown”值,它是声明的值以外的任何值。如果要打印状态的零值为Unknown,请更改常量声明,使Unknown得到零值,即iota。交叉

您使用的是什么操作系统和处理器体系结构? go环境输出

你做了什么? 结果: 你期望看到什么? 你看到了什么? 状态为状态类型。这与字段T.State是否是结构的一部分无关。类型状态的零值为0,这也等于Running。因此,当您将t赋值给t.State未初始化的t实例时,t.State会得到零值,该值正在运行


从代码中我可以看出,State似乎有一个“Unknown”值,它是声明的值以外的任何值。如果要打印状态的零值为Unknown,请更改常量声明,使Unknown得到零值,即iota。

交叉发布:非常感谢您的详细解释,这帮助我摆脱了逻辑混乱。非常感谢。
$ go version
1.13
$ go env
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/chezixin/go"
GOPRIVATE=""
GOPROXY="https://goproxy.io"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/chezixin/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mr/_x323k891dq9yvmvlgwqf4f40000gn/T/go-build490011880=/tmp/go-build -gno-record-gcc-switches -fno-common"
package main

import "fmt"

type T struct {
    Name  string
    Port  int
    State State
}

type State int

const (
    Running State = iota
    Stopped
    Rebooting
    Terminated
)
func (s State) String() string {
    switch s {
    case Running:
        return "Running"
    case Stopped:
        return "Stopped"
    case Rebooting:
        return "Rebooting"
    case Terminated:
        return "Terminated"
    default:
        return "Unknown"
    }
}

func main() {
    t := T{Name: "example", Port: 6666}
    fmt.Printf("t %+v\n", t)
}
t {Name:example Port:6666 State:Running}
Although the string is rewritten, this is related to State.const.
I am printing a T struct in main. The zero value is 0.
But why do you want to display State:Running

When T.State = 0, why does the program think T.State = const.Running?

I understand this now:
String is an interface type, T.State=0, const.Running=0, the two types are the same, the value is the same, the program will think that the two are equal? Doesn't it distinguish between const and struct at the moment?
According to logic, you should distinguish between const and struct, but the program thinks they are equal. Is this a bug? Or am I getting into a misunderstanding?

Can you tell me how to understand it correctly?
thank you very much
Thank you