Suppose I want to define a function with named result parameters, one of which is string. This function internally calls another function that returns a byte representation of such a string.
Is there a way to pass the result without using a temporary variable?
func main() {
out, _ := bar("Example")
fmt.Println(out)
}
func foo(s string) ([]byte, error) {
return []byte(s), nil
}
func bar(in string) (out string, err error) {
tmp, err := foo(in)
if err != nil {
return "", err
}
return string(tmp), nil
}
The idea is that, if possible, I can potentially shorten the code to
func bar(in string) (out string, err error) {
out, err := foo(in)
return
}
Does it make sense?
source
share