0

I'm trying to understand Go modules and create a simple hello world program. Go version: 1.16.2

/project1
/project1/main.go
/project1/helpers/helpers.go

helpers.go will contain some utility method like:

package ???

import "fmt"

func DoSomething() {
  fmt.Println("Doing something in helpers.go")
}

main.go will use methods from helpers.go like this:

package main

import "??"

func main() {
  helpers.DoSomething()
}

VSCode is not allowing me to do this and has a red underline on helpers.

What am I missing here? How can I achieve this?


Edit 1: Adding go.mod and package names:

So I ran go mod init helpers in /helpers folder and came out with this:

/project1/helpers/helpers.go
/project1/helpers/go.mod

go.mod

module helpers

go 1.16

My main.go now looks like this:

package main

import (
    "fmt"
    "helpers"
)

func main() {
    fmt.Println("blah")
    helpers.DoHelperMethod()
}
thepudds
  • 4,787
  • 3
  • 20
  • 37
John Smith
  • 31
  • 1
  • 5
  • What's up with the question marks? Can you please update those to whatever is in the actual code? Does your module have a go.mod file? If so, where is it? Can you share its contents? – mkopriva Mar 27 '21 at 06:20
  • Nothing I put in the ?? seems to make a difference hence I thought that was the issue and looking for hints from the internet :) For discussion, let's say they're both set to "helpers". I don't plan on uploading this code so a FQ GitHub.com URL doesn't make sense here. No go.mod file yet. I assume I actually need 2? One at the `/helpers` folder level and one a the `main.go` level as these will be 2 different modules? – John Smith Mar 27 '21 at 06:24
  • 5
    The tutorial [How To Write Go Code](https://golang.org/doc/code) goes through the steps of writing a program like the one in the question. The question asks about multiple modules, but a single module with multiple packages is a better fit for the code in the question. – Charlie Tumahai Mar 27 '21 at 06:28
  • No you only need one go.mod file per project and that file should be in the root of the project, and go source files inside the `helpers` package should all start with `package helpers` not `package ???` (that ain't even a valid package name). And the import statements for the `helpers` package should look like the following `import "/helpers`. – mkopriva Mar 27 '21 at 06:29
  • 1
    Thanks @mkopriva that gave me the breakthrough I needed. I had it in my mind that I needed two modules but I see now that it's two packages inside on module. I think starting here might be a good start for me! https://golang.org/doc/tutorial/getting-started – John Smith Mar 27 '21 at 06:44
  • With Go 1.18/1.19, and the notion of [Go workspace mode](https://stackoverflow.com/a/68420398/6309), you could use *multiple* `go.mod` in the *same* Go project. – VonC Jul 17 '21 at 12:15

1 Answers1

5

Your project should have only one go.mod file and it should be in the root of the project. You can cd into the project's directory and do go mod init <module_name> where <module_name> in your case can be project1.

For example, once you've initialized the module, your project should look something like the following:

/project1/helpers/helpers.go
/project1/main.go
/project1/go.mod

go.mod

module project1

go 1.16

main.go

package main

import "project1/helpers"

func main() { helpers.DoHelperMethod() }

helpers/helpers.go

package helpers

func DoHelperMethod() {
    // ...
}
mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • To the point answer. Thanks a lot.Btw, I read on internet that general.convention for module_name is to be like, github.com/username/project_name. – Mandroid Jul 09 '23 at 15:42
  • 1
    @Mandroid github.com/username/project_name is just an example of the convention. The [official reference](https://go.dev/ref/mod#module-path) says: *"A module path should describe both what the module does **and where to find it**. Typically, a module path consists of a repository root path, a directory within the repository (usually empty), and a major version suffix (only for major version 2 or higher)."* – mkopriva Jul 09 '23 at 16:46