Different variable behavior and function return value

I want to join two lines, but I get an error.

Original:

hash := sha1.Sum([]byte(uf.Pwd))
u.Pwhash = hex.EncodeToString(hash[:])

Joint:

u.Pwhash = hex.EncodeToString(sha1.Sum([]byte(uf.Pwd))[:])

The first one works fine, the second one displays an error message:

models/models.go:104: invalid operation sha1.Sum(([]byte)(uf.Pwd))[:] (slice of unaddressable value)

Why is this?

+4
source share
1 answer

In the second case, an error message appears because you are trying to cut the return value of a function call ( sha1.Sum()):

sha1.Sum(([]byte)(uf.Pwd))[:]

The return values ​​of function calls are not addressed. As a reminder (only), you can specify the following addresses (taken from Spec: Address Operators ):

... , ; ; . , x (, ) .

, . Spec: :

, , - , .

, , .

, , , , , / .

+9

Source: https://habr.com/ru/post/1609260/


All Articles