How can i get the first item from the return of a function GOLANG

Viewed 32

func processes() ([]Process, error) {
    .... (SOME CODE)

    return results, nil <----------- this return
}

I need take the first value of these return here.

And add it to this func, in the p value.

func PrintProcesses() {
    var p []ps.Process
}
1 Answers

If you wish to append the first value to a slice and check the error, use a temp variable.

var (
    p    []ps.Process
    temp ps.Process
    err  error
)

temp, err = processes()
if err != nil {
    // handle error
} else {
    p = append(p, temp)
}
Related