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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器