登录
首页 >  Golang >  Go问答

Golang 中的时间类型无法被编译器识别

来源:stackoverflow

时间:2024-03-06 18:48:28 312浏览 收藏

golang学习网今天将给大家带来《Golang 中的时间类型无法被编译器识别》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

问题内容

我正在尝试处理 go 中的结构,它有两个属性,我需要将它们作为时间戳:

type Asset struct {
    Owner     string `json:"owner"`
    Key       string `json:"key"`
    StartDate Time   `json:"startDate"`
    EndDate   Time   `json:"endDate"`
    Type      string `json:"type"`
    Amount    int    `json:"amount"`
    Facility  string `json:"facility"`
    State     string `json:"state"`
}

我导入了“time”包,但编译器给出了 time 类型的错误:

“未声明的名称:编译器时undeclaredname”

有什么关于为什么会发生这种情况的提示吗?


正确答案


time 类型在 time 包中定义。您必须 import 该包裹:

import "time"

并使用 qualified identifier 来引用导出的 Time 类型,即 packagename.identifier,在本例中为 time.time

type Asset struct {
    Owner     string    `json:"owner"`
    Key       string    `json:"key"`
    StartDate time.Time `json:"startDate"`
    EndDate   time.Time `json:"endDate"`
    Type      string    `json:"type"`
    Amount    int       `json:"amount"`
    Facility  string    `json:"facility"`
    State     string    `json:"state"`
}

尝试 Go Playground 上的工作代码。

参见相关:Getting a use of package without selector error

当你定义带有时间戳的结构体时,它应该是time.time

参考https://golang.org/pkg/time/和时间结构https://golang.org/src/time/time.go?s=6278:7279#L117

今天关于《Golang 中的时间类型无法被编译器识别》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>