Answer the question
In order to leave comments, you need to log in
How to make a package in golang?
Hello. Can you please tell me how to make a package for Go?
I just started to study and configure the project as I stumbled upon understanding the creation of a package.
> echo $GOROOT
> /home/sergey/go
directory structure
package main
import "fmt"
import "test"
func main() {
xs := []float64{1,2,3,4}
avg := test.Average(xs)
fmt.Println(avg)
}
package test
func Average(xs []float64) float64 {
total := float64(0)
for _, x := range xs {
total += x
}
return total / float64(len(xs))
}
Answer the question
In order to leave comments, you need to log in
imports runtime: cannot find package "runtime" in any of:
indicates that the standard libraries cannot be found, which means that either GO itself is installed crookedly or the paths are crookedly spelled out.
What gives
go env?
On a canonical Go installation, the ~/go/src/pkg/ $GOROOT/src/pkg/ directories should not exist at all. My advice - demolish everything and install cleanly from the source. The 'install from source' procedure is well documented and is rarely difficult. GOROOT will default to /usr/local/go (you can of course expand to home too ). GOPATH, although it can be inside GOROOT , does not have to, they are not related at all. Inside GOROOT and GOPATH there will be src and pkg directories - for sources and built libraries, respectively. pkg should not be inside src.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question