I prefer to work on Linux, but sometimes I use Windows to coordinate with other people or for specific purposes. Here I’ll briefly cover how to get started with OpenGL in Go (golang) on Windows.
First, install a relatively recent version of Go for Windows if you don’t already have it. I recommend taking the latest from the official Go downloads page (removing an old version if needed), and following their directions to test the installation with a “Hello, World!” program to make sure it’s working.
Next, install MinGW. My choice on Windows 10 x64 was MinGW-w64. It’s a large download (hundreds of MBs), but it works. Once the installation is complete, edit environment variables (control panel -> system) and add the MinGW installation directory’s /bin directory to your path. Test by trying
g++ from cmd or PowerShell and see whether it’s a known command.
Configure Your Editor for Go
Pick your code editor of choice and configure it for Go, or install and set up your favorite Go IDE.
Vim Setup Tips
For me that’s usually Vim, so I use gVim on Windows. I used PowerShell to
mkdir .vim in my home directory (because Windows 10 will complain that it doesn’t have a filename, only an extension, if you try to do it through the GUI). Download Tim Pope’s pathogen there (
~/.vim/autoload/pathogen.vim), create a
~/.vim/bundle directory and download other vim plugins there. I used vim-go (all the files and directories should be
~/.vim/bundle/vim-go/* in this setup) to configure vim for Go development and add syntax highlighting. And I added these lines to my vimrc:
" set vim runtime for plugins set runtimepath=~\vimruntime,C:\Users\username\.vim,$VIMRUNTIME " trigger pathogen execute pathogen#infect()
If you’re used to Linux, like me, note that you enter visual block mode with Ctrl + q instead of Ctrl + v on Windows unless you remap.
Get Go Packages and Write First Program
GLFW makes it much easier to start developing an OpenGL application, it has a current bindings package for Go, and I don’t know of any reason not to use it. First,
go get the package from PowerShell, then save the sample script under “Usage” in the go-gl readme as a
.go file, and then build it in PowerShell with
go build <filename>. Once it’s built, execute it, it should work (just opening up a window with a black canvas that should stay open until you close it).
You can also
go get and
import a version of OpenGL core or compatibility to use in your program, e.g.
github.com/go-gl/gl/v4.5-core/gl. If you’ve got an older graphics card and you’re not sure what version to start with, you can look up the version supported by your graphics card or just start with a somewhat older version and it will probably work for getting started.
That’s what you need to get started. Now the rest begins – learning OpenGL if you don’t know it, and figuring out how to access and use all the OpenGL C stuff from Go. And (oh yeah) writing an interesting OpenGL application and doing something with it.
OpenGL C Stuff in Go
For that second part – accessing and using OpenGL C from Go – here are a few more tips.
When you read Khronos’ OpenGL documentation, you’ll notice that command names, constants, and types are prefixed with
GL, respectively. In Go, you’ll use the
gl package to access them, so those prefixes are left off and you’ll use the
gl. qualified identifier prefix instead. You might look at package.go, for example, to get an idea.
Add a Logger
A good starting point is to add a log file so you can print out informational messages if in debug mode (you probably want that off in any default release configuration). Just import log and create a log file like you would in any other Go application.
Because it’s Windows, I noticed
log.Println doesn’t actually create Windows newlines (
\r\n). That’s a slight annoyance if you’re viewing your log in Notepad, but you can create a new logger type, embed
log.Logger into it, create a new
Println method that calls
log.Printf(“%v\r\n”), and use an instance of that type as your logger if you really want to use Notepad. The easier solution is to use a more advanced editor (like Vim or Notepad++) that understands Unix-style line endings as well.
Print the OpenGL Version
Once you’ve got your logger set up, you might start with logging a line upon starting execution of the program (so you know you got that far). After that, it’s nice to have your program log the version of OpenGL being used, and possibly the video driver vendor and version. Here are a few lines of Go code that print version information.
Note that get functions ending in “v” like
gl.GetIntegerv return void and expect to be passed a pointer, similar to how you use the Scanner interface in go’s
fmt package. Major and minor version constants are available in OpenGL versions that aren’t super old.
gl.VERSION should be available in all OpenGL versions, and I found it also included my video card driver vendor and version. Also note that strings coming from C need some conversion to work as strings in Go:
var major, minor int32 gl.GetIntegerv(gl.MAJOR_VERSION, &major) gl.GetIntegerv(gl.MINOR_VERSION, &minor) myLogger.Printf("%v.%v\n", major, minor) myLogger.Printf("%s\n", gl.GoStr(gl.GetString(gl.VERSION)))
For more sample code, I suggest you look at examples in the gl package, such as cube.go.
Isolate Windows-Specific Code
If you prefer Linux, as I do, or might someday want your OpenGL application to work on other platforms, it’s a good idea to isolate any platform-specific code. That’s one of the selling points of OpenGL (and now Vulkan, which you can probably get working from Go too, see glfw issue #182 and vulkan-go) – it should work on many platforms. You may be able to avoid any platform-specific code, but if you need it, make sure it’s isolated. That’s generally good practice in development anyway.
The majority of an OpenGL application is the interesting code that you write for physics, animation, modeling, etc., and most architectures will isolate those areas from other parts of the code. If you’re writing a library and tests that don’t require a GUI, keep in mind you can test and debug those in other environments as well, perhaps just an ssh connection and a tmux session away.
If you’re ready to learn and do more, there are many OpenGL guides in C and C++. A good next step might be to require a minimum version of OpenGL for your program to run. This guide can show you how to do that: open.gl. Other useful links: