I thought I’d try to keep track of the bugs I have in my Golang programs, including compile errors. These aren’t bugs in Go, but bugs I wrote in Go.
=
instead of :=
(7×)import "io"
(3×)range
(2×)regexp.Compile
returned a Regexp
rather than
a *Regexp
(2×)name
as string
because it
was declared as name string
, and the variable data
as byte
because it was declared as data []byte
(2×)return
at the end of a function that had already
assigned its return values (2×)%#v
instead of %+v
even though I didn’t want hexadecimal
printing of my struct fields (2×)import "log"
import "strconv"
import "fmt"
print()
output goes to stdout, not stderrprint
argument lists[]byte
to print
, which rendered as hex garbageioutil.ReadFile
with a file instead of a filenameioutil.ReadFile
to return a string instead of a []byteioutil.ReadFile
on a 1GB file on a machine with only
4GiB of RAMrange
in a for
Regexp.FindAll
with no count argumentRegexp.FindAll
with Regexp.FindAllIndex
print
callint
to int64
explicitlyFile.Name
instead of File.Name()
in a print
,
printing out a hex stringos.Args
suffixarray.Index.Write
, which the
compiler doesn’t catchFile.Read
copy(result[8:10], ...)
and then result[11] =..
not noticing
the skipped item 10bytes.Buffer
instead of a *bytes.Buffer
as an io.Writer
putTo
method, which returns an error, as if it
were Write
, returning two thingsbytes.Buffer.Write
instead of twopanic()
with multiple arguments as if it were
print()
or log.Fatal()
width
and height
rather than
the constants I intendedlog.Fatal
instead of log.Fatalf
, which produced a
particularly confusing error because the last argument happened to
contain a "\r", overwriting most of the error message on the screenbinary.Write
, since I was
writing to a bytes.Buffer
, but it was actually trying to tell me,
“binary.Write: invalid type *main.NServerInit”.binary.Write
on a struct whose fields weren’t
exported (no, that isn’t a bug, never mind; the bugs were those
below)binary.Write
on a struct containing a pointer to
another struct, instead of containing the other struct by valuebinary.Write
on a struct containing a []byte
encoding/binary
after all! But for binary.Read
, not
binary.Write
.type_SetPixelFormat
and then tried to case
SetPixelFormat:
Reader.Read
was reading into a buffer of size 1, and
in the other case, io.ReadFull
is guaranteed to read the right
number unless it fails (which I was handling already)%+v
instead of %#v
even though I did want hexadecimal
printing of my struct fieldsvar
statementself.
, as if I were in Java or C++log.Printf
instead of log.Print
and got this %!(EXTRA)
error in my log message:
2018/09/13 19:51:58 closing %!(EXTRA net.TCPConn=&{{0xc82006a0e0}}, string= because of error , errors.errorString=EOF, string= in , string=message-type)io.ReadFull
var x map[foo]bar
= { ... }
but var x = map[foo]bar { ... }
log.Printf
and got a %!q(MISSING)
error
in my log messagex, y
:= uint32(x), uint32(y)
at the top level of a function, which
totally doesn’t work:=
instead of =
, unintentionally shadowing an outer-scope
variable with one of a different typebinary.Read
by value instead of passing a
pointer, which resulted in an error not caught until runtime (which
caused the VNC server to drop the connection)format.big_endian_flag
when I meant format.Big_endian_flag
string.SplitN
instead of strings.SplitN