cannot compile a test program in Go - windows

compiler: http://code.google.com/p/go/downloads/detail?name=go1.0.3.windows-386.zip&can=2&q=
I've unpacked it to d:\, then made another directory d:\testgo, where I put two files:
The code:
package main
import "fmt"
func main() {
fmt.Println("Hello world!")
}
and the CMD file to run the compilation:
SET PATH=%PATH%;D:\go\bin
go build test.go
#pause>nul
And I got no exe but this:
test.go:3:8: import "fmt": cannot find package
package runtime: import "runtime": cannot find package
So what went wrong ?

The documentations says the following:
If you chose a directory other than c:\Go, you must set the GOROOT
environment variable to your chosen path.
SET GOROOT=d:\Go should do it.

Related

Golang Module problem--package xxx/xxxx is not in GOROOT

so here is what my directory is:
go
|-src
|-ppppppSample
|-newFolderOne
|-firstSample.go
|-hello.go
|-go.mod
and here is the content of hello.go
package main
import (
"fmt"
jjj "ppppppSample/newFolderOne"
)
func main() {
fmt.Println("start to test")
fmt.Println(jjj.FirstVVVV)
}
here is the content of firstSample.go
package newFolderOne
var FirstVVVV = "Im first SSSSSSSSSSSS"
and here is my go.mod's content
module mmmmmppppp
go 1.15
when giving it the cmd go run hello.go, the terminal came out with like this:
D:\Users\eien_zheng\go\src\ppppppSample>go run hello.go
hello.go:5:2: package ppppppSample/newFolderOne is not in GOROOT (C:\Go\src\ppppppSample\newFolderOne)
So here is my question:
(since I'm new for golang, I wish you guys could understand and tolerate some of my misunderstanding)
According to my understanding to Go module (maybe it's wrong), the Go module's function is gonna let some kind of online resource be downloaded to the directory GOPATH/pkg/mod instead of existing in GOROOT.
No matter which directory your project in, your project can still import those resource from GOPATH/pkg/mod if you init Go module.
But!!, in my understanding, it still can use package system to import package around project directory, in the meantime import online resource by Go module system.
How is that when I do (mod init) for hello.go, then it loses the (basic package import function) for this project?
|--src
|--sample
|--newFolder
|-firstSample.go (package xyz)
|--hello.go (package main import(xyz "sample/newFolder")
|--go mod (module sample go 1.15)
go mod should reference the root folder , here the root folder is |--sample
module sample
go v1.xx
inside hello.go;
package main
import ( xyz "sample/newFolder")
and make sure exported functins or variables use camelCase aka starts with BlockLetters.
Import packages within a module using the module's path:
package main
import (
"fmt"
jjj "mmmmmppppp/newFolderOne"
)
...
Run it on the Playground.

Golang package is not in GOROOT (/usr/local/go/src/packageName)

Hi could someone help me run my main.go: go run main.go ?
There are two folders, which are next to each other:
proj1 folder has main.go, go.mod
package1 folder has package1.go, go.mod, utility.go
inside of main.go:
package main
import (
"package1"
"fmt"
)
func main() {
y := package1.Struct1{
v: "1",
}
z := package1.isTrue()
fmt.Println(z)
}
inside my package folder: package1.go
package package1
type Package1 struct {}
func (a *Package1) IsTrue() bool {
return true
}
My Go version: go1.15.2 linux/amd64
My Go env settings:
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user1/.cache/go-build"
GOENV="/home/user1/.config/go/env"
GOMODCACHE="/mnt/sda5/gopath/pkg/mod"
GOOS="linux"
GOPATH="/mnt/sda5/gopath"
GOROOT="/usr/local/go"
...
I tried :
go install, go build ... results no error inside the package folder
go mod vendor, go run main.go, go get -u package1 ... result in the same message when run inside the proj1 folder:
package package1 is not in GOROOT (/usr/local/go/src/package1)
The VS Code Go Plugin Linter shows no problem.
Please help. Thank you!
To solve the error i was facing package package1 is not in GOROOT (/usr/local/go/src/package1)
I had to ensure the environment variables were correctly configured.
I added those lines in the bashrc file:
export GO111MODULE=on
#GOPATH MUST BE OUTSIDE OF GOROOT directory!!!
export GOPATH=/mnt/sda1/programming/gopath
export PATH=$PATH:$GOPATH/bin
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
I loaded the bashrc file in the terminal:
source ~/.bashrc
Now i can execute following procedure to program with the Go language.
Make a new main folder...
Inside this main folder:
make main.go file begin with package main
Run the command below:
go mod init main
make another folder with the new package name: e.g. package1
inside the package1 folder:
make all files with package package1 in its 1st line ... but DO NOT MAKE MOD FILE inside this package folder!!!
in your main.go, you can import that package and use it
import "main/package1"
y := package1.Struct1{a: 1, b: 2,...}
z := y.func1()
This page is the top search result for...
package xxxxx is not in GOROOT (/usr/lib/go/src/xxxxx)
In my case, I was trying:
go build mything
That gave me the error. The solution was simple, I needed to be more specific:
go build mything.go
I faced the same problem but in my case i forgot the .go so after running :
problem : go run main
solution: go run main.go
and now it works fine

Can't find custom package

I'm tryng to create a custom package in go: i created folder project:
my_project
|_database
|_database.go
main.go
but when i try to import it gaves me this error: "could not import database (no package for import database)"
i try to run "go init" as written in some tutorial and created a "go.mod" file, then i run "go install" and works fine, but in the main.go it still not working.
The project structure is now this:
my_project
|_database
|_database.go
|_go.mod
|_go.sum
main.go
I work on windows with visual studio code
Here is how i import the package:
import (
"fmt"
...
"database"
)
function main() {
You need to do a few things.
Make sure your database.go is the following way:
it should contain a package name this case is database, comment the
function/s that need to be used outside this package, use func instead
of function. and comment code and capitalize function name so it can
be exported.
package database
import "fmt"
// Connect function to connect to my database
func Connect(){
fmt.Print("connected...")
}
your main.go should be the following.
use func instead of function, import database based on folder position
this case is ./ use the package name database. because this function
does not belong from current package main.
package main
import "./database"
func main() {
database.Connect()
}
This should be your folder structure.
my_project
|_database
|_database.go
main.go
go.mod
go.sum

How do I fix VS Code error: "Not able to determine import path of current package by using cwd" for Go project

I'm following tutorials and I think I may have missed something.
I have a Go project located at:
/Users/just_me/development/testing/golang/example_server
The contents are:
main.go
package main
import "fmt"
func main() {
fmt.Println("hi world")
}
I have a ~/go directory.
go env shows:
GOPATH="/Users/just_me/go"
GOROOT="/usr/local/Cellar/go/1.12.9/libexec"
I installed the suggested packages in VSCode.
When I save my main.go I get:
Not able to determine import path of current package by using cwd: /Users/just_me/development/testing/golang/example_server and Go workspace:
/Users/just_me/development/testing/golang/example_server>
How do I fix this?
Since your package is outside of $GOPATH, you may need to create a module file.
You'll need to init your go module using
go mod init your.import/path
Change the import path to whatever you like.
This way you set the import path explicitly, which might help fix it.
The resulting go.mod file looks like this:
module your.import/path
go 1.14 // Your go version
So if the go.mod file is in the same directory as a main.go file, you can now import submodules from it:
E.g. main.go:
package main
import (
"your.import/path/somepackage" // Import package from a subdirectory. This only works if `go.mod` has been created as above
)
func main() {
somepackage.SomeMethod()
}
And in somepackage/whatever.go:
package somepackage
import "fmt"
func SomeMethod() {
fmt.Println("Success!")
}
if you are using vs code, check if the go and code runner extensions are enabled, if enabled, try disabling and enabling again, and if not, install and enable, and download all requested packages.

Relative imports in Go

I have a go Project with the following directory structure
utils(pkg)
| auth.go (has a function names test1)
controllers(pkg)
| login.go (has a function names test2)
I am trying to access function test1 from login.go. Here is what I have done
import "../utils"
func test2(c *gin.Context) bool{
utils.test1()
}
But I always get Unresolved reference test1. I am new to go . Can anyone help why I am getting this error?
No there is no relative import in Go.
you should use the absolute path considering GOPATH:
The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code. To get started, create a workspace directory and set GOPATH accordingly. see: https://golang.org/doc/code.html#GOPATH
Import paths
An import path is a string that uniquely identifies a package. A package's import path corresponds to its location inside a workspace
or in a remote repository (explained below).
The packages from the standard library are given short import paths
such as "fmt" and "net/http". For your own packages, you must choose a
base path that is unlikely to collide with future additions to the
standard library or other external libraries.
If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For
instance, if you have a GitHub account at github.com/user, that should
be your base path.
Note that you don't need to publish your code to a remote repository
before you can build it. It's just a good habit to organize your code
as if you will publish it someday. In practice you can choose any
arbitrary path name, as long as it is unique to the standard library
and greater Go ecosystem.
Example:
This example assumes you have set GOPATH=/goworkdir in your OS environment.
File: goworkdir/src/project1/utils/auth.go
package utils
func Test1() string {
return "Test1"
}
File: goworkdir/src/project1/controllers/login.go
package controllers
import "project1/utils"
func Test2() string {
return utils.Test1()
}
File: goworkdir/src/project1/main.go
package main
import (
"fmt"
"project1/controllers"
)
func main() {
fmt.Println(controllers.Test2())
}
Now if you go run main.go you should see output:
Test1
This is now different since the introduction of go modules, from go 1.11.
Thus, if you switch to go modules, and if your module is called "m", then the idiomatic way to do relative imports in your project tree would be to use: import "m/utils" and import "m/controllers" in places where you need to import those packages in your project.
For details, see:
https://github.com/golang/go/wiki/Modules#do-modules-work-with-relative-imports-like-import-subdir
GoLand users - by default these forms of imports appear as errors in the IDE. You need to enable Go Modules integration in settings
Here is another example project structure with file contents required to import correctly:
test1/
utils/
texts.go
main.go
go.mod
with following contents:
go.mod:
module mycompany.com/mytest
go 1.15
utils/texts.go (to make a function visible from a different package, it needs to start with an uppercase letter):
package utils
func GetText() string {
return "hello world"
}
main.go (only the full import name is supported, there is no shortcut to import from the same module easier):
package main
import (
"fmt"
"mycompany.com/mytest/test1/utils"
)
func main() {
fmt.Println(utils.GetText())
}
Given this directory configuration:
.
├── controllers
│   └── login.go
├── main.go
└── utils
└── auth.go
File main.go:
package main
import "./controllers"
func main() {
controllers.Test2("Hello")
}
File controllers/login.go:
package controllers
import "../utils"
func Test2(msg string) {
utils.Test1(msg)
}
File utils/auth.go:
package utils
import . "fmt"
func Test1(msg string) {
Println(msg)
}
Result works:
$ GO111MODULE=auto go build -o program main.go
$ ./program
Hello
So what you wanted to do works. The only difference is that I've used upper case function names, because it's required to export symbols.
I think you can just crate a vendor directory next to your source file, which acts like a relative GOPATH, and then create a relative symlink, which links to the package you want to import inside the vendor directory, and then import the package as if the vendor directory is your $GOPATH/src/.
It's possible as of Go 1.16, although still not as straightforward as it could be, by editing the go.mod file to resolve the package name to a relative path:
if you have a hello and a greeting packages side by side (hello contains main.go):
<home>/
|-- greetings/
|--go.mod <- init this as infra/greetings
|--greetings.go <- let's say that this package is called greetings
|-- hello/
|--go.mod <- init this as whatever/hello
|--main.go <- import infra/greetings
then edit hello's go.mod file and get the package:
go mod edit -replace infra/greetings=../greetings
go get infra/greetings
go mod tidy
There's a full example in the official documentation but hey, this might change again in the next version of Go.

Resources