使用iota时,逻辑混乱。那是错误吗?

问题描述 投票:0回答:1

您正在使用哪个版本的Go(go version?]

$ go version
1.13

此问题会在最新版本中重现吗?

您正在使用什么操作系统和处理器体系结构(go env

[go env输出

$ 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
go
1个回答
0
投票
我们为什么要显示State:Running?当T. State = 0时,为什么程序想T. State = const。正在运行?


Effective Go
在设计数据结构时,无需进一步初始化就可以使用每种类型的零值。这意味着数据结构的用户可以创建一个...并且得到工作的权利。

在Go中,零值应具有含义。在这种情况下,默认为未知。


const ( Running State = iota + 1 Stopped Rebooting Terminated )

例如,

package main

import "fmt"

type T struct {
    Name  string
    Port  int
    State State
}

type State int

const (
    Running State = iota + 1
    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)
}

操场:https://play.golang.org/p/7vOiiewVvTU

输出:

t {Name:example Port:6666 State:Unknown}

© www.soinside.com 2019 - 2024. All rights reserved.