doulongsi1831 2017-01-13 17:51
浏览 1091
已采纳

Golang无法将(类型* string)转换为类型字符串

When I tried to compile the following code I get following errors:

users.go:31: cannot convert pass (type *string) to type string

users.go:78: cannot convert &user.Password (type *string) to type []byte

How do I dereference or convert the pointer to the string literal?

Thanks in advance.

code which I am trying to compile: https://play.golang.org/p/gtMKLNAyNk

  • 写回答

1条回答 默认 最新

  • doulin8374 2017-01-13 18:01
    关注

    The if on line 9 needs to change I think. user.Username and user.Password are strings so they will never be nil. What you need to check instead is for empty string like this: if user.Username != "" && user.Password != "" {

    pass := &user.Password password := []byte(*pass)

    Don't take the address of user.Password. Just use password := []byte(user.Password)

    Basically all of your issues are variations on that theme.

    Start fixing this issue by removing all & and * from your code and it will probably work (except for this one: ctx.Get("database").(*gorm.DB))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求