Universal scope and importing variables from one file to another - go

I am new at Golang. I am learning about universal scope and importing variables from one file to another.
Conditions:
System: Windows 11 x64
IDE: VS code
Language: GoLang
According to the universal scope I should be able to import variables from one file to another easily. Primary file: "main.go" and secondary file ""uniscope.go
Importing from uniscope.go into main.go
If the files are kept in the same folder directory then I can easily import using command: "go run main.go uniscope.go"
This runs without any error but until this command I get the error in main.go, "undeclared name", that is still not the main issue.
When I move the uniscope.go to another folder and then I run "go run main.go uniscope.go", it doesn't work, which is understandable as directory has been changed. So then I copy the path and paste it in import which in turns resolves the previous error, change the command to "go run main.go" but it throws out a new one.
"main.go:6:7: illegal character U+0073 's' in escape sequence"
I am not sure how I am wrong here. The video I am learning from is: https://www.youtube.com/watch?v=vYD9XWi_Xw8&list=PLve39GJ2D71xX0Ham0WoPaYfl8oTzZfN6&index=5
He is using itelliJ but I don't think that should be an issue.
I have tried the mentioned above and also tried finding the exact error on google. Just found a similar question posted on some Portuguese forum with no answers.

main.go:
package main
import "hello/world"
func main() {
println(world.Value)
}
world/world.go:
package world
const Value = 1
and run:
go mod init hello
go run .

Related

How do you call a Go function in VSCode?

I have VSCode and the Go plugin installed. I am trying to call this function:
package main
import "fmt"
func printHello() {
fmt.Println("hello world")
}
First, I opened the VSCode Command Palette and chose Go: Test file, which threw a No tests found. Current file is not a test file. error.
Then I opened the VSCode Command Palette and chose Go: Test function at cursor - this threw the same error.
Then I created a sibling file with a _test.go naming convention:
package main
import (
"testing"
)
func TestHello(t *testing.T) {
msg, err := printHello()
}
This threw a build error: c:\path\to\my_test.go:8:18: main() used as value.
Then I added a simple return statement, which threw the following error:
go: cannot find main module, but found .git/config in c:\path\to\my_folder
to create a module there, run:
cd ..\.. && go mod init
I right-clicked the function name itself and chose the Go: Debug Test at Cursor function, and the Go: Generate unit tests for function option, which in-turn prompted me to install several other VSCode plugins, and prompted me to upgrade Go itself. The previous two Command Palette options started to fail with ENOENT errors, so I restarted VSCode, and got a new test function. I opened up the VSCode Debug Console but it wouldn't let me start a session there.
I right-clicked the parent folder and chose the Open in Integrated Terminal option, which started a WSL session - cd ..\.. && go mod init threw errors related to GOPATH:
me#COMPUTER:/mnt/c/path/to/my_folder$ go mod init
-bash: /c/go/bin/go.exe: No such file or directory
I tried determining what the WSL GOPATH was pointing to via Go: Show current GOPATH - it actually turned out that both the WSL and VSCode GOPATHs were incorrect.
I opened up a normal Command Prompt which had the correct GOPATH and I fired off go mod init which errored out (the previous guidance from VSCode was incorrect):
go: cannot determine module path for source directory C:\path\to\my_folder (outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
Run 'go help mod init' for more information.
I decided on a v2 Go module initialization (I don't know why one would opt for v0 or v1), which created a go.mod in the root directory of my project, but now I am seeing the following package-related errors:
gopls requires a module at the root of your workspace.
You can work with multiple modules by opening each one as a workspace folder.
Improvements to this workflow will be coming soon, and you can learn more here:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.
How do I call a Go function in VSCode?
The answer turns out to be:
You need to create a *_test.go unit test harness and install more adjacent VSCode plugins.
You need to run go mod init blah somewhere outside of VSCode. blah is the name of your module. VSCode is unreliable with respect to the GOPATH.
There is a gotcha here where if you run go mod init main, this is valid but VSCode will throw misleading errors later on (see https://appliedgo.net/testmain/). You have to name it something else.
You need to re-open the VSCode workspace in a way such that go.mod is in the root of the workspace, or else VSCode runs into relative pathing errors.

Go install exclude file

I have created a go script that compiles, starts, checks the status, and ends a web service I created (that is also in go). However, I have come to a road block.
With the compile feature I run the following command:
go install .
Which gives the following error:
./script.go:55: main redeclared in this block
previous declaration at ./hello.go:8
Which makes sense as I have two different files, both with the main func and main package. I also tried moving the script to another folder and then changing the command ran to:
go install {path}
Where {path} is equal to the path I want installed/compiled. Which I then got the following error:
exit status 1: can't load package: package /var/www/test.com/go: import "/var/www/test.com/go": cannot import absolute path
So in conclusion I have thought of only one solution (and I am up to hear others if mine isn't the best approach). My idea is to exclude the script file from compiling with the rest of the files, but I am unsure how to.
I did some research and couldn't find an easy way to do it (such as an --exclude flag with the go install command). Does anybody know how to accomplish what I am trying to achieve?
Thank you.
you could give the hello.go a different package name, that should work. Or i am missing something?
Regards
Tim

"go get golang.org/x/tools/go/gcimporter15" fails on undefined identifiers

I'm trying to make a program dependent upon gcimporter15 for Go, and so I'm using the command "go get golang.org/x/tools/go/gcimporter15", but it fails with the error:
# golang.org/x/tools/go/gcimporter15
../../go/src/golang.org/x/tools/go/gcimporter15/bexport.go:557: undefined: constant.ToFloat
../../go/src/golang.org/x/tools/go/gcimporter15/gcimporter.go:396: pkg.SetName undefined (type *types.Package has no field or method SetName)
That appears to me that there is an error within gcimporter itself, but that doesn't make sense that I would get this when it doesn't appear that others are. Why isn't it working?
I'm using Go 1.5.3.
The godoc.org/golang.org/x/tools/go/gcimporter15 tells that the package gcimporter is deprecated and this package will be deleted in October 2017. And this new code should be used: golang.org/x/tools/go/gcexportdata instead of gcimporter .
So, what you have to do is:
$ go get godoc.org/golang.org/x/tools/go/gcexportdata
PS: Tested within Ubuntu 16.04 64bit and go version go1.6.2 linux/amd64 without any issues.
Test:
package main
import (
"fmt"
gcexportdata "golang.org/x/tools/go/gcexportdata"
)
func main() {
filename, path := gcexportdata.Find("fmt", "")
fmt.Println(filename, path)
}
Output:
/usr/lib/go-1.6/pkg/linux_amd64/fmt.a fmt
Its bad but seems there is a mismatch between the gcimporter15 and go-1.5. I faced the same problem and looked at go-1.5 code in file src/go/constant/value.go and there is no ToFloat() function in the constant package.
As #nexus66 says, the gcimporter is deprecated. So, I don't expect them to fix this. If you are using this package directly, its better to move on to the recommended gcexportdata package. If you are using a third party library which is in turn using gcimporter, may be you should upgrade to go-1.7. That's what I did and things worked fine.

goconvey Please run goconvey from within your $GOPATH cannot import absolute path

I'm setting up go and trying to get a simple project working with http://goconvey.co/
I have my $GOPATH set to /Users/joe/Desktop/playground/go
and when I run
$ go get github.com/smartystreets/goconvey
it downloads all good to my GOPATH
so when I create a project here
/Users/joe/Desktop/playground/go/some-project
and run goconvey I get
2015/02/04 14:41:05 shell.go:93: Please run goconvey from within your $GOPATH
My testing code is
package main
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestStuff(t *testing.T) {
Convey("Truth", t, func() {
Convey("is falsey", func() {
So(false, ShouldBeFalse)
})
})
}
I don't know why it connot find the files.
When I run go test it works perfectly.
Help?
All go code needs to be within $GOPATH/src/ for the GoConvey UI to work.
So, if your $GOPATH is set to
/Users/joe/Desktop/playground/go
then you will need to put your project at
/Users/joe/Desktop/playground/go/src/some-project
Your code is currently at
/Users/joe/Desktop/playground/go/some-project
Having said all that, the error message should probably be modified to read something like this:
Please run goconvey from within $GOPATH/src (also, symlinks might be problematic).
The name of the variable referenced by #VonC is probably a slight misnomer in this case.

Q: Getting Build Error "Invalid Import Path"

I'm stuck on running the BeeGO app using "bee run" it says
The thing is I've already have setup properly my GOPATH to D:/Web Dev/GO/BeeGO/test-project/
and also routers path does exist and I've tried to manual build the file but it doesn't generate an .exe file.
Anyone knows how to fix this?
I'm using Windows 8.1 Pro (64-bit)
Thanks
GO expects the directory structure under $GOPATH in following ways as described in code organization:
$GOPATH/src <--- where your source code goes
/pkg
/bin
Instead of placing your source files directly under $GOPATH (D:/Web Dev/GO/BeeGO/test-project/ for your case), you want to move your code under $GOPATH/src.
D:/Web Dev/GO/BeeGO/test-project/src/main.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/routers/routers.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/controllers/controllers.go
import path should be always starting from $GOPATH/src. routers.go can be always imported as import "quickstart/routers" and controllers.go can be imported as import "quickstart/controllers".
That's not how you import a package.
The import path is relative to $GOPATH/src. use:
import "quickstart/routers"
Finally fixed the bug from the framework,
What I did:
in main.go import from
"D:/Web Dev/GO/BeeGO/test-project/quickstart/routers"
I changed it to _"../quickstart/routers" make sure to include _ this means to import the library even if it is not used,
Then in the routers/router.go I changed the import path
"D:/Web Dev/GO/BeeGO/test-project/quickstart/controllers" to "../controllers"
It seems BeeGO doesn't generate the template properly and caused the build to fail.
Another possiblity for this error, is when you copy-paste code from the internet,
and
import "quickstart/routers"
became
import "quickstart/routers "
due to bugs in some CMS/Blog systems (notice the space at the end before the closing quote...).

Resources