英文:
Go: undefined class
问题
我想尝试使用Go语言的图形库。我找到了以下的示例代码:
package main
import (
"image"
"image/color"
"image/png"
"log"
"os"
)
func main() {
width, height := 512, 512
canvas := NewCanvas(image.Rect(0, 0, width, height))
canvas.DrawGradient()
// Draw a series of lines from the top left corner to the bottom of the image
for x := 0; x < width; x += 8 {
canvas.DrawLine(color.RGBA{0, 0, 0, 255},
Vector{0.0, 0.0},
Vector{float64(x), float64(height)})
}
outFilename := "lines.png"
outFile, err := os.Create(outFilename)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
log.Print("Saving image to: ", outFilename)
png.Encode(outFile, canvas)
}
然而,在构建时似乎缺少一些类。
D:\go\work>go build draw.go
# command-line-arguments
.\draw.go:13: undefined: NewCanvas
.\draw.go:19: undefined: Vector
.\draw.go:20: undefined: Vector
我的环境在HelloWorld示例中工作正常,但在导入图像库时似乎缺少一些内容。有什么帮助可以帮助新手入门吗?
英文:
I'd like to try graphics libraries with Go. I've found the following example:
package main
import (
"image"
"image/color"
"image/png"
"log"
"os"
)
func main() {
width, height := 512, 512
canvas := NewCanvas(image.Rect(0, 0, width, height))
canvas.DrawGradient()
// Draw a series of lines from the top left corner to the bottom of the image
for x := 0; x < width; x += 8 {
canvas.DrawLine(color.RGBA{0, 0, 0, 255},
Vector{0.0, 0.0},
Vector{float64(x), float64(height)})
}
outFilename := "lines.png"
outFile, err := os.Create(outFilename)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
log.Print("Saving image to: ", outFilename)
png.Encode(outFile, canvas)
}
However, when building it seems that some Classes are missing.
D:\go\work>go build draw.go
# command-line-arguments
.\draw.go:13: undefined: NewCanvas
.\draw.go:19: undefined: Vector
.\draw.go:20: undefined: Vector
My environment worked fine for the HelloWorld example, however it seems something is missing when importing image libraries. Any help to get started a newbie ?
答案1
得分: 2
你需要按照它们的依赖顺序包含每个.go
文件。
尝试按照以下顺序运行主文件,假设你正在使用这个库:https://github.com/felixpalmer/go_images
go run draw.go canvas.go vector.go
英文:
You need to include each .go
files in their dependency order.
Try running the main files in this order supposing that you are using this library: https://github.com/felixpalmer/go_images
go run draw.go canvas.go vector.go
答案2
得分: 1
你缺少了NewCanvas
函数和Vector
结构体,它们来自你找到的示例所在的同一个包。我相信你在这里找到了它们1。
你可以运行go get https://github.com/felixpalmer/go_images
并添加你缺少的导入。
英文:
You're missing NewCanvas
function and Vector
struct from the same package where you found that example. I believe you found it here.
You can just run go get https://github.com/felixpalmer/go_images
and add imports you missing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论