如何使用自定义包

I have follow two simple go files

main.go

package main

import "fmt"
import "go-package/math"

func main() {
    xs := []float64{1, 2, 3, 4}
    avg := math.Average(xs)
    fmt.Println(avg)
}

mymath.go

package math
func Average(xs []float64) float64 {
    total := float64(0)
    for _, x := range xs {
        total += x
    }
    return total / float64(len(xs))
}

The file and directory structure is:

$GOPATH
  src
    go-package
       math
         mymath.go
       main.go

When I navigate to $GOPATH/src/go-package, and open the terminal and run the command: go run main.go, the result is printed out.

Then, I navigate to $GOPATH/src/go-package/math, and run the command go install, and the package successfully installed at $GOPATH\pkg\windows_amd64\go-package\math.a

Then I navigate back to $GOPATH/src/go-package, and remove the math directoy, I rerun the command go run main.go, error occurs:

main.go:4:8: cannot find package "go-package/math" in any of:
        D:\softwareInstalled\Go\src\go-package\math (from $GOROOT)
        D:\softwareInstalled\Go\GoPath\src\go-package\math (from $GOPATH)

I have put the math package at $GOPATH\pkg\windows_amd64\go-package\math.a

I wonder why Go doesn't pick up the package from this directory.

And how can I use math.a?Where should I place it?

Golang uses the source files of your imported packages. So when you wanna use the package go-package/math inside your main.go, you should not delete the source files from src/go-package/math. Leave them inside your project and everything's good.

Besides, go get also loads source files into your $GOPATH/src folder. Hope this helps.