Golang CGO inside Windows

What is CGO?

CGO lets Go packages call C code

Installing MSYS2

  • Download installer from https://www.msys2.org/ (current it’s msys2-x86_64-20200903.exe)

  • After install, inside MSYS terminal, using pacman, install gcc

pacman -S \
 mingw-w64-x86_64-binutils \
 mingw-w64-x86_64-crt-git \
 mingw-w64-x86_64-gcc \
 mingw-w64-x86_64-gcc-libs \
 mingw-w64-x86_64-gdb  \
 mingw-w64-x86_64-headers-git \
 mingw-w64-x86_64-libmangle-git   \
 mingw-w64-x86_64-libwinpthread-git \
 mingw-w64-x86_64-make \
 mingw-w64-x86_64-pkg-config \
 mingw-w64-x86_64-tools-git \
 mingw-w64-x86_64-winpthreads-git \
 mingw-w64-x86_64-winstorecompat-git 
  • now, configure bash initialization
# inside: ~/.bashrc

alias ll="ls -la --color"

export PATH=/s/go/bin:$PATH
export GOPATH=/w/work/go

Configure Windows Terminal

As explained in official guide…

{
 "guid": "{17da3cac-b318-431e-8a3e-7fcdefe6d114}",
 "name": "MINGW64 / MSYS2",
 "commandline": "S:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64",
 //"startingDirectory": "C:/msys64/home/%USERNAME%",
 "startingDirectory": "W:/work",
 "icon": "S:/msys64/mingw64.ico",
 "fontSize": 12,
 "colorScheme": "GitBash"
},
{
 "guid": "{2d51fdc4-a03b-4efe-81bc-722b7f6f3820}",
 "name": "MINGW32 / MSYS2",
 "commandline": "S:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw32",
 // "startingDirectory": "C:/msys64/home/%USERNAME%",
 "startingDirectory": "W:/work",
 "icon": "S:/msys64/mingw32.ico",
 "fontFace": "Lucida Console",
 "fontSize": 12
},
{
 "guid": "{71160544-14d8-4194-af25-d05feeac7233}",
 "name": "MSYS / MSYS2",
 "commandline": "S:/msys64/msys2_shell.cmd -defterm -here -no-start -msys",
 // "startingDirectory": "C:/msys64/home/%USERNAME%",
 "startingDirectory": "W:/work",
 "icon": "S:/msys64/msys2.ico",
 "fontFace": "Lucida Console",
 "fontSize": 14
}

Testing

cd $GOPATH
go get github.com/mattn/go-sqlite3
go install github.com/mattn/go-sqlite3