4

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

5
  • Can you also post the exact lines that are giving you issues in addition to your snippet. The lines don't match up so it's hard to know where the issue is. Commented Jan 13, 2017 at 17:54
  • Also add the models.User struct definition to your snippet, might be helpful Commented Jan 13, 2017 at 17:55
  • @shieldstroy here you go gist.github.com/itsbalamurali/e46d10acdceda906925d0e4836e9023f Commented Jan 13, 2017 at 17:57
  • You keep unnecessarily taking the address of things in that code. Hint, &user.Username will never be nil. Commented Jan 13, 2017 at 17:59
  • @JimB very new to this pointers thingy & go :) Commented Jan 13, 2017 at 18:02

1 Answer 1

6

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))

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Good luck! In general you can not worry too much about pointers until the compiler tells you that you need to. That's not entirely true but you'll get the hang of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.