我不知道如何创建一个包并使用它。我正在使用 liteid 并转到 1.4.2 但这都可以从命令行重现。我能够创建看起来的形状包,但它不会从主包加载。GOPATH=d:\src\teaching\golangGOROOT=c:\go+teaching\golang\pkg \windows_386 shape.a \src \packages packages.go \shape shape.gogo install shape -> generates shape.ago build packages.go# packagesd:\src\teaching\golang\src\packages\packages.go:5: imported and not used: "shape"d:\src\teaching\golang\src\packages\packages.go:8: undefined: Shaped:\src\teaching\golang\src\packages\packages.go:19: undefined: Circle形状.gopackage shapeimport ( "fmt")const ( pi = float64(3.14))type Shape interface { Area() float64}type Circle struct { x int y int radius int}func (c *Circle) Area() float64 { return pi * float64(c.radius*c.radius)}func (c Circle) String() string { return fmt.Sprintf("{x=%d, y=%d, radius=%d}", c.x, c.y, c.radius)}包.gopackage mainimport ( "fmt" "shape")func calculateArea(shapes ...Shape) float64 { sum := float64(0) for _, v := range shapes { sum += v.Area() } return sum}func main() { circle := Circle{x: 1, y: 2, radius: 2} fmt.Println(circle, circle.Area(), calculateArea(&circle))}有任何想法吗?
- 1 回答
- 0 关注
- 193 浏览
添加回答
举报
0/150
提交
取消