Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang Cannot Convert (type *string) to type 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

like image 391
itsbalamurali Avatar asked Sep 02 '25 08:09

itsbalamurali


1 Answers

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

like image 96
shieldstroy Avatar answered Sep 04 '25 23:09

shieldstroy