📜 ⬆️ ⬇️

“Errors are values” in Go and echo VB

Fate brought me (a programmer's practitioner, mostly using C #) on a project in which the main functionality is developed on Go.

Studying Go drew attention to the unusual practice of error handling. After reading the explanations in the articles, Errors are values and in Why “errors are values” in Go noted that the solutions proposed there make us recall one feature of Visual Basic that programmers did not really appreciate.

The essence is as follows. There are complaints from programmers about error checking in Go. Take an example from the article Errors are the values :

_, err = fd.Write(p0[a:b])
if err != nil {
    return err
}
_, err = fd.Write(p1[c:d])
if err != nil {
    return err
}
_, err = fd.Write(p2[e:f])
if err != nil {
    return err
}
// and so on

, , , . , try-catch ?

:

func (ew *errWriter) write(buf []byte) {
    if ew.err != nil {
        return
    }
    _, ew.err = ew.w.Write(buf)
}

w := &errWriter{w: fd}
ew.write(p0[a:b])
ew.write(p1[c:d])
ew.write(p2[e:f])
// and so on
if ew.err != nil {
    return ew.err
}


, . , .

, , - . Visual Basic If. Visual Basic , .

Visual Basic If C++ Java ?:. , :

Dim a As Integer
If CheckState() Then
	a = 12
Else
	a = 13
End If

VB : , inline. , , IIf:

a = IIf(CheckState(), 12, 13)

, , . , , IIf , , , , If:

a = IIf(CheckState(), GetTrueAValue(), GetFalseAValue())

, : GetTrueAValue GetFalseAValue. , IIf , If ( ?: ++), .. , , .

, IIf If, IIf , . , .. . , IIf , , , .

If Visual Basic ( ) , . IIf – .

. Go , . , , IIf Visual Basic. :

w := &errWriter{w: fd}
ew.write(getAB())
ew.write(getCD())
ew.write(getEF())
// and so on
if ew.err != nil {
    return ew.err
}

, : ew.write(getAB()) , getCD() getEF(), ?

? , — , , . Go.

_, err = fd.Write(getAB())
if err != nil {
    return err
}
_, err = fd.Write(getCD())
if err != nil {
    return err
}
_, err = fd.Write(getEF())
if err != nil {
    return err
}

if. if , . , , .

, ty-catch-finally , , , .

P.S.: VB:

On Error Resume Next

. , .

')

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


All Articles