I don’t even know how to google (I found something like this only in if, it still didn’t work to enter). Explain, please, what err means in these places and how the value assigned to err appears in the variables in front of err:
1. src, err := os.Open(srcName, os.O_RDONLY, 0)
2. if r, err := f1(i); err != nil if r, err := f1(i); err != nil
A similar construction was observed with ok instead of err.
In the documentation found this:

 if v := math.Pow(x, n); v < lim { return v } 

However, everything is clear, unlike the examples above.

    2 answers 2

    There is about the same thing that you found in the documentation, it just additionally overlapped with multiple returns and multiple assignments . "Value" (in fact, not one) is assigned not to err , but decomposed (piece by piece) across the entire list of variables.

    Here is an example of using this feature of the language with Go By Example with freely translated comments:

     package main import "fmt" // (int, int) описывает возвращаемое значение как "два значения типа int" func vals() (int, int) { return 3, 7 } func main() { // Два значения разошлись по разным переменным, вот множественное присваивание a, b := vals() fmt.Println(a) fmt.Println(b) // Неиспользуемые значения можно присвоить в пустоту, обозначаемую _ _, c := vals() fmt.Println(c) } 

    You have the same thing, just the types are different.

      If you look in the documentation, the following description is given there.

       func Open(name string) (*File, error) 

      This description says that the function returns a type of 2 values. The first is the file itself, and the second is an error. The assignment operator deconstructs this type into 2 variables.

      A simpler example of using the assignment operator can be viewed and run here . Here is his full code.

       package main import ( "fmt" ) func main() { a, b := 5, 6 fmt.Println("a =", a) fmt.Println("b =", b) } 

      After assignment, you can check the variable in which the error is placed ( err != nil ) and process it.

      • And thank you, and the person above. Could have - put two jackdaws, but so it is impossible :) - Flerry
      • one
        the main thing is that one set. - Mikhail Vaysman