Some tips with Go and Gogland

Hello to all. I am very new to Go and Gogland. I have a project Go project in Gogland

  • I choose "Run kind" as Package - to run not only the main file, but also the project. Why can't it find the main package?
  • How do I import the util.myprinter package into main.go in order to use it?

Please help me

+3


source to share


1 answer


First, the general structure of your Go workspace seems to be wrong. You need to make it look like this:

D:
|-- go_projects
|    |-- bin
|    |-- pkg
|    |-- src 
|    |    |-- FirstSteps
|    |    |    |-- main.go
|    |    |    +-- util
|    |    |         +-- myprinter.go
|    |    |-- SecondProject
|    |    |-- ThirdProject
...

      

Second, your statement import

appears to be empty, I don't know how goglang works, but if you want to use whatever is in your file myprinter.go

, you will need to import the package util

, assuming the myprinter.go

file declares it package

as util

above.

// FirstSteps/main.go
package main

import (
    "FirstSteps/util"
)

func main() {
    util.MyPrinterFunc()
}

      



And, of course, to be able to use any of them util

, there must first be something ...

// FirstSteps/util/myprinter.go
package util

func MyPrinterFunc() {
    // do stuff...
}

      

Edit . Sorry, I didn't actually answer your question. You are getting the error Cannot find package 'main'

because of the wrong workspace setup, which I mentioned earlier. Package path

tells Gogland where the package you want to run is relative to the directory $GOPATH/src

. Therefore, after configuring your wrokspace correctly, you should set Package path

to FirstSteps

, since the absolute path of the package will be $GOPATH/src/FirstSteps

. If you later want to run the package util

, you must specify Package path

how FirstSteps/util

so that it can find it.

+8


source







All Articles