go: go.mod file not found in current directory or any parent directory; see 'go help modules'

1,807 views
Skip to first unread message

Bima lely

unread,
Mar 25, 2024, 12:25:12 PM3/25/24
to golang-nuts
Can you help me? i am getting problems when running other program packages. This error appears "go: file go.mod not found in current directory or any parent directory; see 'go help module'", P3.PNGP5.PNGP4.PNG but when I try on my friend's laptop there is no error. I'm confused. I've been experiencing this for several weeks. I have attached the file along with a screenshot. Hopefully someone from my brothers and sisters can help me. Thank You.

TheDiveO

unread,
Mar 25, 2024, 12:44:53 PM3/25/24
to golang-nuts
If I'm not mistaken, then the module statement in your go.mod is incorrect. It needs to specify the import path for your module. As you're trying to import a submodule using "golang-book/chapter11/math" your module statement should be "module golang-book/chapter11" if I'm not mistaken. However, this then looks like a stdlib import path, IDK if this causes further problems. Usually, something like "golang-book.com" avoids clashing with stdlib, but you need to update all corresponding imports in your example.

Otherwise, please do not post screenshots. You'll only raise dangerous gophers.

Kurtis Rader

unread,
Mar 25, 2024, 2:59:46 PM3/25/24
to Bima lely, golang-nuts
Please don't use screenshots for plain text. They are hard to read and we can't copy/paste the text in the image. Does running `go mod tidy` fix the problem?

Also, using "main.go" as the module name will work but the module name is usually a domain such as "example.com".




On Mon, Mar 25, 2024 at 9:25 AM Bima lely <bima...@gmail.com> wrote:
Can you help me? i am getting problems when running other program packages. This error appears "go: file go.mod not found in current directory or any parent directory; see 'go help module'", P3.PNGP5.PNGP4.PNG but when I try on my friend's laptop there is no error. I'm confused. I've been experiencing this for several weeks. I have attached the file along with a screenshot. Hopefully someone from my brothers and sisters can help me. Thank You.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8780900e-3e88-424e-bf39-77559c0b2292n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Wojciech S. Czarnecki

unread,
Mar 28, 2024, 6:54:23 PM3/28/24
to golan...@googlegroups.com
Dnia 2024-03-25, o godz. 11:58:57
Kurtis Rader <kra...@skepticism.us> napisał(a):

> Please don't use screenshots for plain text.

Uhhh... and do delete images on the reply, please.

That said: Thank you for reacting to screenshots posted :)

--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE

Cleberson Pedreira Pauluci

unread,
Mar 28, 2024, 8:28:00 PM3/28/24
to golang-nuts
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
Reply all
Reply to author
Forward
0 new messages