I am trying to get values from “custom” package
from %GOPATH%/src/custom/custom.go and print that in main.go file. I have go extension installed in vs code. It removes imports because the package is not correctly imported.
I don’t understand why this isn’t working. I have checked the %GOPATH% and it looks like this “F:\Super\Super\Workspace”
my src folder looks like this
Hi, I mistakenly posted without adding /src/ path in my post now I edited it. That’s how I structured custom.go before and changed variable’s first name to UPPERCASE as you’ve said in $GOPATH/src/custom/custom.go. But the error still saying this. I’m not using go modules.
go run main.go
.\main.go:12:22: undefined: custom.Varone
This works on paper. However, in practical (as Go communities experienced in the past), the dependencies management was horrible. Say if you want push your main.go package to Github, you need to move the directory into github.com/ and etc.
It was so annoying to the point of creating various workarounds like vendor/ directory, then godep and so on. Then, one day, Go dev team had enough and solve the entire dependency horror with go module feature.
I once initialized go mod after my last reply so there is gomod path in the env. This is what I got:
set CGO_CFLAGS=-g -O2
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\user\AppData\Local\Temp\go-build551321694=/tmp/go-build -gno-record-gcc-switches
Okay, it’s not workspace problem and very likely related to a faulty (can be mis-configured or was not designed for handling GOPATH) vscode editor. Close the old project and delete it your workspace. Your codes are already backup in this thread anyway.
To avoid wasting your learning resources (time and effort) for both long and short run, I will guide you on how to use go module instead of the classical GOPATH. The reason being you won’t be able to get more supports in the future if you stick to the old way.
Go Module Guide
1. Start An Entirely New Project in “My Documents” (outside of GOPATH)
Proceed to create a directory in your "My Documents" as long as it is outside of GOPATH. This will be your new workspace.
FYI, Go module was designed to work outside of GOPATH first the start. Hence, moving forward, you shouldn’t be meddling with GOPATH unless you’re dealing with classical out-of-date Go packages (which you won’t be using them anyway).
The name is recommended to be the same as your package name. There are some rules to it such as:
keep everything lowercase
recommend keeps it to 2-3 syllabus
no space, dash, or underscore
For this example, I will use the package name myproject.
2. Initialize Go Module
Once done, on the terminal, get inside your workspace directory. You always work inside your root workspace.
$ cd <your workspace directory> # Always stay in your workspace's root directory.
Then proceed to initialize the Go module. The command is as follows:
$ go mod init <module name>
The <module name> field varies based on use case. If you’re using Github or Gitlab, then, the field itself is the Github/GitLab pathing to the package.
For this example, I will use my GitLab address so the command is:
$ go mod init gitlab.com/holloway/myproject
If everything is successful, you should get a go.mod file inside your workspace.
go.mod file content:
3. Set Replace Clause (Optional)
To keep the development experience easier, it’s better to set a replace clause and reference your module back to the current working directory. This is to prevent the go module from keep pulling from your remote server every time you compile your codes.
To do that, edit your go.mod and add a replace clause:
replace <module name> => ./
If we continue the example from above, the output should be something like:
replace gitlab.com/holloway/myproject => ./
At this point, you have a clean working Go module.
4. Test the Go Module with Hello World App
Let’s proceed to test the module. To start, create cmd/<package name> directory inside the workspace to house your main.go package. Then, proceed to create a simple “Hello World” main.go inside it and go run it.
If we follow the example above, you should get something like:
Many thanks. Sorry for late reply. After your suggestion I’ve chosen to use go modules. I’ve seen and tried go modules before even in official go sandbox. But it did not work on vscode on current install. For now I’ve set up a virtual machine in Linux. Go seems to be working just fine with vscode.