doubaben7394 2019-02-27 17:48
浏览 174
已采纳

如何使用golang库中的回调函数?

I'm still relatively new to Go and I've decided to try some OpenGL since there exists a Go library which provides bindings for this. As part of setting up OpenGL I need to use the Go glfw library. I need to use the glfw library function SetFramebufferSizeCallback which requires an argument of type FrameBuffersizeCallback; so I essentially need to pass a function that matches that type's signature which I believe I have done. However, I keep getting the following error:

./main.go:43:35: cannot use fbCallback (type func(*glfw.Window, int32, int32)) as type glfw.FramebufferSizeCallback in argument to window.SetFramebufferSizeCallback

And I'm really not sure why this is happening. I've checked that my glfw version matches the documentation; I've checked the code for the implementation on github and my machine and it is all consistent. So I have no clue what I'm doing wrong. Here is my code, it shouldn't do anything interesting if built, but demonstrates the error:

package main

import (
    "github.com/go-gl/gl/v3.3-core/gl"
    "github.com/go-gl/glfw/v3.2/glfw"
    "log"
    "runtime"
)

func fbCallback(window *glfw.Window, width int32, height int32) {
    gl.Viewport(0, 0, width, height)
}

func init() {
    runtime.LockOSThread()
}

func main() {
    err := glfw.Init()
    if err != nil {
        panic(err)
    }

    defer glfw.Terminate()

    glfw.WindowHint(glfw.ContextVersionMajor, 3)
    glfw.WindowHint(glfw.ContextVersionMinor, 3)
    glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

    window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil)

    if err != nil {
        panic(err)
    }

    window.MakeContextCurrent()

    if err := gl.Init(); err != nil {
        log.Fatalln(err)
    }

    gl.Viewport(0, 0, 800, 600)
    window.SetFramebufferSizeCallback(fbCallback) //Does not work
}

My understanding of how this should really work is demonstrated in the following short program which works. I'd be happy for someone to point out why this short example works and the above does not. I may have some holes in my understanding.

package main

import (
    "fmt"
)

type callback func(int, int) int

func sum(x int, y int) int {
    return x + y
}

func useCallback(cb callback) {
    result := cb(2, 3)
    fmt.Println(result)
}

func main() {
    useCallback(sum)
}
  • 写回答

1条回答 默认 最新

  • douxu5845 2019-02-27 17:56
    关注

    https://github.com/go-gl/glfw/blob/2484f3e51bc432ea9f7daa182e418d5522201176/v3.2/glfw/window.go

    type FramebufferSizeCallback func(w *Window, width int, height int)
    

    vs

    func fbCallback(window *glfw.Window, width int32, height int32)
    

    Your function doesn't have the same signature, it is using int32 instead of int.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势