goimport

Cannot import go-gl library - "build constraints exclude all Go files"


I'm very new to golang, sorry if this is obvious. I've followed the steps from the project Github, and here is my exact procedure (with error):

$ go mod init mydomain.com/test
$ go mod tidy
$ go get -u github.com/go-gl/gl/v4.6-core/gl
$ go run .
package evlis.org/nemo
        imports github.com/go-gl/gl: build constraints exclude all Go files in C:\Users\myname\go\pkg\mod\github.com\go-gl\gl@v0.0.0-20231021071112-07e5d0ea2e71

Why won't it find the go-gl package??

Here's the go.mod file:

module evlis.org/nemo

go 1.22.4

require github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a
require github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71

And here's my full main.go:

package main

import (
    "log"
    "runtime"

    "github.com/go-gl/gl/v4.6-core/gl"
    "github.com/go-gl/glfw/v3.3/glfw"
)

const (
    width  = 500
    height = 500
)

func main() {
    runtime.LockOSThread()

    window := initGlfw()
    defer glfw.Terminate()

    program := initOpenGL()

    for !window.ShouldClose() {
        draw(window, program)
    }
}

// initGlfw initializes glfw and returns a Window to use.
func initGlfw() *glfw.Window {
    if err := glfw.Init(); err != nil {
        panic(err)
    }

    glfw.WindowHint(glfw.Resizable, glfw.False)
    glfw.WindowHint(glfw.ContextVersionMajor, 4) // OR 2
    glfw.WindowHint(glfw.ContextVersionMinor, 1)
    glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
    glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

    window, err := glfw.CreateWindow(width, height, "Conway's Game of Life", nil, nil)
    if err != nil {
        panic(err)
    }
    window.MakeContextCurrent()

    return window
}

// initOpenGL initializes OpenGL and returns an intiialized program.
func initOpenGL() uint32 {
    if err := gl.Init(); err != nil {
        panic(err)
    }
    version := gl.GoStr(gl.GetString(gl.VERSION))
    log.Println("OpenGL version", version)

    prog := gl.CreateProgram()
    gl.LinkProgram(prog)
    return prog
}

func draw(window *glfw.Window, program uint32) {
    gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
    gl.UseProgram(program)

    glfw.PollEvents()
    window.SwapBuffers()
}

I've searched online and the specific repo for similar issues, closest I found was this, however go clean -modcache does not help in my case...


Solution

  • Many thanks to @Brits for pointing me in the right direction.

    The issue was not having the gcc compiler on my system. To fix, I downloaded GCC 14.1.0 Win64 - without LLVM/Clang/LLD/LLDB, added the extracted mingw64\bin to my user path environmental variable, and re-ran the following go commands:

    $ go env -w "CGO_ENABLED=1"
    $ go run .
    

    Success.