Problem in setting the value of GOPATH on windows machine - go

My GOROOT path :-
C:\Go
I have set GOPATH to :-
C:\Users\kunal\go
But when I import modules (like github.com/gorilla/mux) inside VS Code. VS Code prompts me this error :-
could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of C:\Go\src\github.com\gorilla\mux (from $GOROOT) C\src\github.com\gorilla\mux (from $GOPATH) \Users\kunal\go\src\github.com\gorilla\mux (from $GOPATH))
From above It is clear that it shows me two different GOPATH which I haven’t set. How do I fix this?

I recommend going through the following path, using official documentation pages:
Read about properly installing Go for your platform
Read the getting started tutorial which also tells you how to install 3rd-party packages and use them in your code.
It should take you no more than 20 minutes to go through these steps, and it's almost certain that you'll be able to accomplish your goal by the end of the process. As a bonus, keep going through the Getting Started guide beyond the first page to learn how to create your own Go modules, use them from other modules, write tests, build your code into a binary, and more.
This is IMHO the minimal background required to even try writing Go programs; without going through these steps, you will lack crucial fundamental understanding and it will be hard to even understand SO answers.
Specifically in your case - please remember that at this time (with Go 1.16), GOPATH is pretty much deprecated and you should be using Go modules instead. The documents linked to above will explain this in detail.

I ll give a TLDR for the solution given by #Eli Bendersky. If you don't understand GOPATH and go modules, you can look it up yourself here. Here I have assumed you are using VS Code with golang extension. I haven't tested it for other IDEs but it should work in a similiar way.
In the source directory where you have the main.go, create a file named go.mod
Name the package name to whatever you like and save the file.
go to your terminal and run go build main.go, this will download all the missing packages(if any) and will update the go.mod file and create a new file go.sum to create the checksums of the package versions.
All the package error squiggles should be gone by now in your IDE, if it doesn't try restarting your IDE once. You are good to go!
If you are stuck somewhere, let me know in the comments.

I just started to learn golang today. But it seems that I understood more from what these experts are trying to say when trying to answer this basic question.
I don't claim deep knowledge of golang. But as a 2100 rated chess player, I do claim common sense and rigorous thought process. The GOPATH environment variable is very much in use do not believe these kids. Only a kid would make such claims that GOPATH has no use.
The way I see it is that, the go.mod and go.sum plays in tandem with GOPATH.
Continue on your own journey, little tiger. I upvoted your question for moral support.

Related

vscode: Move Go code (type/function) to other file

How to move a function/type from one Go file to an other Go file in vscode?
... and the IDE should update all usages of the moved function/type ...
In the past I used Python and PyCharm, and it supported this kind of refactoring.
It seems that this feature (the move-refactoring) hasn't been implemented in VSCode yet. The only workaround that just came into my mind is this.
You can use the cmd gomvpkg to move a package to a new one. Thanks to this, if you have only the function within the file you should be good to go. It should also update the import statements (make sure to double-check them anyway). I know that it's boring in fact I keep copying/pasting functions between packages as it's faster.
It seems that this feature is already supported in Goland, so you can give it a try maybe if you wish.
Let me know!

How would I use Coldfire to write an AV evasion program in Go?

A bit of background:
I am a pentester who is looking for new ways to get around AV detection during tests / bounties and I recently found the Go library "Coldfire" on Github. I am new to Go so I was wondering how I would structure a project with this package.
It doesn't give much instructions except the func that it uses and I am coming from Python.
https://github.com/redcode-labs/Coldfire
If anyone can give me some tips, I am trying to write an AV evasion program that will kill AV processes and possibly disable a WAF.
You don't have to give me full code or anything, I just would like some examples on how to use the package is all. Thank you so much and please excuse my lack of knowledge I love Go but am very new to it and trying to learn it respective to my profession.
Not really interested in Disruptive functions but with the recent outbreak in Go malware I would love to understand how malicious attackers use those functions too if possible.
I imported the library but it kept giving me an error saying I wasn't using the package even though I had implemented some of the functions to see what they did.
Not sure what goes in said func like..
func PkillAv()
Not sure what would go in the {} on this one or if I would even need it.
You would just need to import it at the top part of your code like:
import "github.com/redcode-labs/Coldfire"
You also need to run
go mod tidy
To download this package.
After that you can use the functions provided in the package. There is no "structure" that you need to manualy create

vscode: How to search in installed package?

I want to search in the go pkg go-git.
ctrl+shift+f searches only in my module.
Probably this will not directly answer, but it's an workaround, that I personally prefer:
Hovering over a function, that is from a foreign package, VS Code shows the godoc description. There is always a link to pkg.go.dev (This link is for the PlainClone example):
Following it you are redirected directly to GO's package website.
Here is already a nice list with the module's functions.
if you want to dive deeper:
On the top of the repository there is the link to the repo host.
Experiences show, that this is mostly Github. While GitHub's search used to be proscribed, it can nowadays be a mightful tool:
https://docs.github.com/en/search-github/searching-on-github/searching-code#search-within-a-users-or-organizations-repositories
Again I mention that is very subjective, but GO's complex way of storing modules locally with it's many env variables and stuff made me feel using the internet is more comfortable =)

Go does not recognize installed library using go get

So I am working with JSON in golang and I want to use this package in GitHub https://github.com/icza/dyno so i use the go get github.com/icza/dyno command to get the package.
but when i run my code this error show
d:\Repos\GoProjects\src\github.com\Timothy Ganoza\sbx\main.go:8:2: cannot find package "github.com/icza/dyno" in any of:
C:\Program Files\Go\src\github.com\icza\dyno (from $GOROOT)
d:\Repos\GoProjects\go\src\github.com\icza\dyno (from $GOPATH)
exit status 1
Process exiting with code: 1
I recommend going through the following path, using official documentation pages:
Read about properly installing Go for your platform
Read the getting started tutorial which also tells you how to install 3rd-party packages and use them in your code.
It should take you no more than 20 minutes to go through these steps, and it's almost certain that you'll be able to accomplish your goal by the end of the process. As a bonus, keep going through the Getting Started guide beyond the first page to learn how to create your own Go modules, use them from other modules, write tests, build your code into a binary, and more.
This is IMHO the minimal background required to even try writing Go programs; without going through these steps, you will lack crucial fundamental understanding and it will be hard to even understand SO answers.
If problems still persist, feel free to update the question but provide more details: how are your go env vars set up (output of go env), are you using modules (you should!), how does your module directory look, etc.

Golang requirements.txt equivalent

Coming from a python/django world, it'd be great to have something like a requirements.txt equivalent for go/revel. How can I do this? I know I can just write a requirements.txt file and then do something like
cat requirements | xargs go get
But what if my requirements ALSO have requirements? The above command would attempt to "go get" them, and then they'd fail to build, since I don't have those requirements installed.
Is there something I'm missing?
The command go get does exactly what you need: It finds all dependencies and downloads and installs the missing ones. Focus on "all": go get really traverses your dependency graph.
Have a look at the documentation:
https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them
The Go documentation is really clean, short and well written. I would recommend always to have a look at the documentation first before making assumptions which are based on experience with other tools or tool-chains.
They also provide useful blog posts, https://blog.golang.org/using-go-modules
I just found that the kubernetes guys actually have created an overview page for themselves here.
Summary is this: Currently stable is Glide and the cool new toy is called dep

Resources