Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imported and not used error

Tags:

package

go

I can't figure out how to create a package and use it. I'm using liteid and go 1.4.2 but this is all reproduce-able from the command-line. I' able to create the shape package it seems but it doesn't load from the main package.

GOPATH=d:\src\teaching\golang
GOROOT=c:\go

+teaching\golang\pkg
                  \windows_386
                    shape.a
                \src
                  \packages
                    packages.go
                  \shape
                    shape.go

go install shape  -> generates shape.a

go build packages.go
# packages
d:\src\teaching\golang\src\packages\packages.go:5: imported and not used: "shape"
d:\src\teaching\golang\src\packages\packages.go:8: undefined: Shape
d:\src\teaching\golang\src\packages\packages.go:19: undefined: Circle

shape.go

package shape

import (
    "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)
}

packages.go

package main

import (
    "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))
}

Any ideas?

like image 548
dvallejo Avatar asked Jan 08 '23 17:01

dvallejo


1 Answers

Shape is defined in the shape package. You have to reference it as shape.Shape

like image 79
JimB Avatar answered Jan 15 '23 02:01

JimB