Golang packages error


(Caleb David) #1

Hello go bridgers, I have a problem with importing packages to golang workspace.
I am trying to import a package from within the same project directory but it’s not loading. Here’s the folder structure:
$GOPATH/src/github.com/microworlds/myapp

myapp
main.go (package main)
mypackage
demo.go (package demo)

When I try to load load the demo package in main, it shows an error like:
[go] cannot find package "mypackage/demo" in any of: /usr/local/go/src/mypackage/demo (from $GOROOT) /home/me/work/src/mypackage/demo (from $GOPATH)

My import in main.go goes like this:
import (
“fmt”
“mypackage/demo”
“log”
“strings”

"github.com/PuerkitoBio/goquery"

)
Why am I getting this kind of error and how do I fix that
Ubuntu 18.04
Sorry for unformated text, I’m new here, Thank you.


(Norbert Melzer) #2

You always need to specify full package path on import, including the guthub.com/username/repository ceremony…


(Caleb David) #3

I did so. I’m using vscode. After specifying the full path, when I save the file, the import statement disappears. I was wondering, must I run a go get on that package to be able to use it? (But I’m not committing the project to github yet)


(Norbert Melzer) #4

Depending on how you configured vscode it will remove packages on save it thinks you do not use. This is a setting of the formatter IIRC… I consider this really annoying…

And you only need to go get if you haven’t the package available already.


(Caleb David) #5

Thank you, it works just fine. All I had to do was to use a different text editor (Brackets).