【问题标题】:no required module provides package SDL2不需要模块提供包 SDL2
【发布时间】:2021-02-18 19:57:12
【问题描述】:

我正在尝试从https://github.com/veandco/go-sdl2 导入 sdl2 包。我遵循了 readme.md for Windows 上出现的所有步骤。安装mingw64并在cmd上执行:

go get -v github.com/veandco/go-sdl2/sdl

它工作正常,但是当我尝试这样做时:

go run .\sdl2.go

我收到了这个错误:

sdl2.go:4:2: no required module provides package github.com/veandco/go-sdl2/sdl: working directory is not part of a module

在我的另一台计算机上运行完美,但是当我克隆此存储库并将软件包安装到另一台笔记本电脑上时,我遇到了很多问题。要获取有关此案例的更多信息,我将我的测试源放在这里以提供更多信息:

package main

import (
    "github.com/veandco/go-sdl2/sdl"
    "fmt"
)

const winWidth, winHeight int = 800, 600



type color struct {
    r, g, b byte
}

func setPixel(x, y int, c color, pixels []byte) {
    index := (y * winWidth + x) * 4

    if index < len(pixels)-4 && index >= 0 {
    pixels[index] = c.r
    pixels[index+1] = c.g
    pixels[index+2] = c.b
    }
}

func main() {
    winWidth := 800
    winHeight := 600

    window,err := sdl.CreateWindow("Testing SDL2", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
        int32(winWidth) ,int32(winHeight), sdl.WINDOW_SHOWN)
    if err != nil {
        fmt.Println(err)
        return 
    }
    defer window.Destroy()

    renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer renderer.Destroy()

    tex, err  := renderer.CreateTexture(sdl.PIXELFORMAT_ABGR8888, sdl.TEXTUREACCESS_STREAMING, int32(winWidth), int32(winHeight))
    if err != nil {
        fmt.Println(err)
        return
    }
    defer tex.Destroy()

    pixels := make([]byte, winWidth*winHeight*4)

    for y := 0; y < winHeight; y++ {
        for x := 0; x < winWidth; x++ {
            setPixel(x,y, color{byte(x % 255),byte(y % 255), 0}, pixels)
        }
    }
    tex.Update(nil,pixels,winWidth*4)
    renderer.Copy(tex,nil,nil)
    renderer.Present()

    sdl.Delay(2000)

} 

【问题讨论】:

  • go run .\sdl2.go 你确定这个反斜杠吗?
  • @bobra 是的,它可以在另一台计算机上使用,我使用了 powershell 的自动完成功能并且可以使用
  • @Dailo141 您的文件中没有声明包,请将package main 添加到文件的第一行
  • 是的,但是我在发帖之前输入错误,我现在刚刚编辑
  • 你有模块初始化吗?你打电话给go mod init my_module_name了吗?

标签: go sdl-2


【解决方案1】:

New module changes in Go 1.16博客

go 命令现在默认以模块感知模式构建包,即使没有 go.mod 存在。这是朝着在所有项目中使用模块迈出的一大步。

按照博客中的建议,将 GO111MODULE 的值更改为 auto go env -w GO111MODULE=auto 将解决问题。

【讨论】:

  • 请注意,blog 还声明“Go 1.17 将忽略 GO111MODULE”;所以设置GO111MODULE=auto 不是一个长期的解决方案(计划使用模块,getting started tutorial 涵盖了基础知识)。
猜你喜欢
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
相关资源
最近更新 更多