package main
import "fmt"
import "os"
func main(){
path := os.Getenv("GOPATH")
fmt.Println(path)
}
This will print empty string in Mac but it will print the path in windows
package main
import "fmt"
import "os"
func main(){
path := os.Getenv("GOPATH")
fmt.Println(path)
}
This will print empty string in Mac but it will print the path in windows
First, check if the GOPATH is exported,
export
if not exported, exec on your project:
cd /var/www/myproject
export GOPATH=$( pwd )
export PATH=$PATH:$GOPATH/bin
so then execute your program again.
export PATH=$PATH:$GOPATH/bin
I have export this before running my program.
But why to export GOPATH it should be set by the golang itself.
No, go does not tamper with your environment. It does only read from it. And if the GOPATH
is set, it will be used. If it is not set, the default of ~/go
will be used, but your program will never know if it was built from the default or any other GOPATH
, it should not care anyway.
But in windows without doing all this we can directly access the GOPATH
I’m wondering why its set at all. Since go 1.9 or even earlier, its not necessary to set the GOPATH
explicitely anymore.
Anyway, if GOPATH
isn’t set, you can either stick to the default value (which depends on your system) or shell out and ask go
by invoking go env GOPATH
.
But why do you wan’t to know the GOPATH
, unless you want to analyze some source code, thats usually not necessary…
package main
import "fmt"
import "os"
func main(){
path := os.Getenv("GOPATH")
fmt.Println(path)
a:= path+"/src/github.com/ibmdb/go_ibm_db/installer"
if _, err := os.Stat(a); os.IsNotExist(err) {
fmt.Println("FOLDER DOES NOT EXIST")
}
if _, err := os.Stat(a); !os.IsNotExist(err) {
fmt.Println("FOLDER EXIST")
}
}
I want to check whether the folder exists or not. If not then I will make some function call.
@akhilravuri
Yep, you can get more on this:
Which folder? GOPATH
is a list, on linux and mac colon (:
) separated and semicolon at windows (;
).
when user downloads my package he will run setup.go file which will download the clidriver zip file and untar it, But before downloading the zip I want to check whether user has that folder or not, If not I will download the zip or else I will skip the process.
I think that you don’t need check if folder exists, you only install the package and use, like this:
In that repository in installer folder setup.go file is there. I was trying to modify that file so that it will not download the clidriver every time when the user downloads the repository.
NO!
Your library might be statically linked in a binary and shipped to a computer with restricted internet access, how do you think that ZIP will be downloaded there?
Just tell the people that use your library, that they need to have the clidriver (whatever that is) also installed and available in PATH.
As a fallback check for an ENV var that specifies the position of that program, if putting it to PATH is not possible for the user.
Just an observation… never do this. Instead use filepath.Join otherwise you will have troubles in moving your application between Windows and Unix/Linux because of the file path separator.
Ontopic, on MacOS put the GOPATH variable in $HOME/.bash_profile
and will be available in your application.
He still can’t assume that I or any client did that setting.
Relying on the GOPATH
to download third party tools is plain wrong. Especially for a library. Instead one should document dependencies properly.
Totally agree, I made the point just to understand why he can’t see the GOPATH
All Other IBM opensource drivers work in the same way(Download the clidriver when a user downloads the opensource driver). So, I have to do that in the same way.
You won’t gain anything by this in go land.
Even if you manage to download the driver to the computer of a developer who wants to use your library, what do you think how that will get deployed to targets?
Go is made in a way, that its generated binaries can be deployed as they are and one do not care about pre-installed libraries or there versions (except for libc vs musl systems).
I doub’t your library will get accepted by the community if it works against how everything else works.
Thanks for information I will look into it.
Thanks
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.