-5

Did I do something wrong on importing a package under src? My folder structure is like it:

 - project
     - src
       helper.go( package utils)
 main.go(package main)

And I want to use utils in the main.go I write this:

import ("utils")

but give me the error,said can't import utils. I don't understand which is wrong. there is no other package under src. Thank you for the help.

LiLian
  • 289
  • 2
  • 14
  • 5
    Show the file location and contents of go.mod. From there, we can tell you how to import the package. [How to Write Go Code](https://golang.org/doc/code) is a good tutorial on the subject. – Charlie Tumahai Mar 20 '21 at 22:45
  • Have a look here: https://stackoverflow.com/questions/57940353/how-to-structure-golang-modules-and-project-structure-in-the-new-way/57944766#57944766 – alessiosavi Mar 20 '21 at 23:06

1 Answers1

2

You must provide only 1 package within single folder. Consider to name package like the folder. In your case create folder "utils" and move your helper.go in there.

Please, don't forget to name public types, vars and funcs properly: start their names with uppercase symbol:

Finally your project would look like this:

project structure

Your helper.go would look like this:

package util

func SomeFunc() {

}

And your main.go would look like this:

package main

import "stackoverflowexamples/src/util"

func main(){
    util.SomeFunc()
}
iddqdeika
  • 91
  • 2