Go: 未定义的类

huangapple go评论79阅读模式
英文:

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 (
	&quot;image&quot;
	&quot;image/color&quot;
	&quot;image/png&quot;
	&quot;log&quot;
	&quot;os&quot;
)

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 &lt; width; x += 8 {
		canvas.DrawLine(color.RGBA{0, 0, 0, 255},
			Vector{0.0, 0.0},
			Vector{float64(x), float64(height)})
	}

	outFilename := &quot;lines.png&quot;
	outFile, err := os.Create(outFilename)
	if err != nil {
		log.Fatal(err)
	}
	defer outFile.Close()
	log.Print(&quot;Saving image to: &quot;, outFilename)
	png.Encode(outFile, canvas)
}

However, when building it seems that some Classes are missing.

D:\go\work&gt;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.

huangapple
  • 本文由 发表于 2016年1月21日 19:43:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/34922901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定