Twitter Facebook Google+ Google+ Email

Golang cheat sheet and various tips

Last modified:
444 Words
22:00 | by Alex Vie in SiteDevelopment
Reading time: approx. 1 minute(s).

GCC-GO linker errors

Sometimes, when using Gcc-go instead of the default Go compiler, an error message like the following may appear during the build command.

[...] $WORK/b001/_pkg1_.a(_x009.o): undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.1'
      /lib/ error adding symbols: DSO missing from command line
      collect2: error: ld returned 1 exit status

This indicates a missing LDFLAG, in our case, the instruction to link against libpthread is missing from the linker command line. It’s easy to fix, just pass the missing -pthread via -gccgoflags to the build command. The -pthread option not only instructs the linker to link against libpthreads, it also defines all macros that are needed to ensure re-entrant code is produced during compilation.

[alex@warpcore:/home/alex/go/src/testgo]$> go-8 build -gccgoflags=-pthread dbtest.go

Note: go-8 is the wrapper to gccgo, using GCC8 on Suse Linux distributions. This might be different for your distribution and depends on the gccgo version. GCC8 based gccgo compiler is compatible with Go version 1.10.

Getting gocode and go-mode in Emacs to work

This is for Go version 1.10. First, install the mdempsky fork of gocode as the old gocode might be incompatible with Go 1.10 due to changes in the binary package format.

The resulting binary will be in $GOPATH/bin (usually ~/go/bin on *iX systems), so you can either add this directory to your $PATH or copy gocode to ~/bin or /usr/bin if you have permissions.

In Emacs, it’s just a matter of installing go-mode. Assuming, you have a working MELPA setup, simply do a M-x package-install <RET> go-mode <RET>. For auto-complete, I prefer company-mode, in which case, the Go backend company-go is required. Install it like go-mode before. Go-mode itself requires minimal configuration - if gocode is in your $PATH it will be found and used automatically.

Note that gocode depends on a properly configured $GOPATH environment variable and may not work at all without it, particularly if you do not use the default go directory, which is normally set to ~/go by default. Also, gocode only recognizes packages built and installed with the default go compiler. Personally, I use gccgo only for release builds and usually install all packages with both compilers.

Sometimes (rarely though), gocode will get stuck and subsequently refuse to offer more completions for reasons unknown. I’ve not yet found a pattern or way to reproduce it. If that happens, simply do a $PATH/to/gocode close (or simply gocode close if it is in your $PATH). This will terminate gocode and emacs will happily re-launch it at the next attempt.