登录
首页 >  Golang >  Go问答

无法将结构元素分配为 Google/uuid 数据类型

来源:stackoverflow

时间:2024-04-27 15:24:58 300浏览 收藏

本篇文章给大家分享《无法将结构元素分配为 Google/uuid 数据类型》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

我在我的项目中使用 https://github.com/google/uuid,我希望我的 user struct 有一个 id 作为 uuid,但它不允许我将其分配为数据类型。当我尝试时,它给出了错误 syntax error: unexpected :, waiting type。这是我的代码供参考:

package postgres

import (
  "time"
  "github.com/google/uuid"
)

type DbUser struct {
  ID: uuid.UUID,
  Username: string,
  Password: string,
  Email: string,
  DateOfBirth: time,
  dateCreated: time, 
}

任何人都可以帮助我阐明将结构元素或变量作为 uuid 传递的语法吗?


解决方案


您的 struct definition 错误,您使用的是 composite literal 的语法。 应该是:

type dbuser struct {
  id uuid.uuid
  username string
  password string
  email string
  dateofbirth time.time
  datecreated time.time
}

另请注意,time 不是类型,而是包名称。

你可能想拿Tour of Go来学习基本语法。

time是包名,不能定义字段,你应该使用time.time。

import (
      "time"
      "github.com/google/uuid"
    )
    
    type DbUser struct {
        ID          uuid.UUID
        Username    string
        Password    string
        Email       string
        DateOfBirth time.Time
        dateCreated time.Time
    }

以上就是《无法将结构元素分配为 Google/uuid 数据类型》的详细内容,更多关于的资料请关注golang学习网公众号!

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