Hi Bima, prefer text instead of prints. It's faster to help somebody.
There's two ways to fix that problem.
1 - You need to use the same module name when import it. As the module name is "main.go" (it's not recommend name like this) you need call it in the import statement like this:
import(
"fmt"
math "main.go/math"
)
2 - You can rename the module name in the go.mod file to golang-book/chapter11 (This module name is better than main.go) and use this name as import to your code.
This is an example of how it might be written.
chapter11/
└── math
└── math.go
├── go.mod
├── main.go
go.mod
module golang-book/chapter11
go 1.22.0
math.go
package math
func Average(xs []float64) float64 {
total := float64(0)
for _, x := range xs {
total += x
}
return total / float64(len(xs))
}
main.go
package main
import (
"fmt"
"golang-book/chapter11/math"
)
func main() {
xs := []float64{1, 2, 3, 4}
avg := math.Average(xs)
fmt.Println(avg)
}
Result:
2.5
You can read this great tutorial:
https://go.dev/doc/tutorial/create-module.
I hope I was able to help you :)
Cleberson Pauluci