Dante
Dante

Reputation: 11264

How to specify the types of multiple return values

Sometimes i find Go code hard to read when a function returns multiple values and their types are not specified. Is this possible ? See below:

func randomNumber()(int, error) {
    return 4, nil 
}

func main() {
    nr, err := randomNumber()
    // What i would like to do:
    // var nr int, err error = randomNumber()
}

Upvotes: 1

Views: 142

Answers (1)

Daniel
Daniel

Reputation: 38761

No, that's not possible. However, you could define the variables ahead of time to make it a little easier to follow.

func randomNumber()(int, error) {
    return 4, nil 
}

func main() {
    var nr int
    var err error
    // Note the '=' instead of ':='
    nr, err = randomNumber()
}

Upvotes: 4

Related Questions