How to setup VSCode for golang/go cmd module? - go

It works well for std module (golang/go root directory) on VSC. However it doesn't work on cmd module (opened from src/cmd directory). There were no code navigation, symbol search, and even any problems/errors reported.
Version info:
vscode: 1.61.0
vscode-go: v0.28.1
Installed go tools:
gopkgs
go-outline
gotests
gomodifytags
impl
goplay
dlv
dlv-dap
staticcheck
gopls

Open VSC from $WORKSPACE/go. Follow the instruction of https://github.com/golang/tools/blob/master/gopls/doc/advanced.md#working-on-the-go-source-distribution as #hana mentioned:
Compile the Go binary. Change working directory to $WORKSPACE/go/src and run ./make.bash (or ./make.bat for Windows cmd). It will generate the $WORKSPACE/go/bin directory.
Set "go.goroot" to "${workspaceFolder}" and install all Go tools required.

Related

Not able to watch go files after installing Compile Daemon

Installed using, go get github.com/githubnemo/CompileDaemon and go install github.com/githubnemo/CompileDaemon
When I try to run it using -> CompileDaemon --compile="./folderName"
It gives -> zsh: command not found: CompileDaemon
Note:
I'm using oh-my-zsh.
My GOPATH and GOBIN are not default one's, I set it to goWorkspace
Install Compile Daemon using
"go install github.com/githubnemo/CompileDaemon#latest"
Important:
Ensure that the Compile Daemon executable path(i.e: GOBIN) is included in $PATH.
You can include it by editing your ".bashrc" or ".zshrc" file.
or by "export PATH=$PATH:$GOBIN"
First, don't forget that the use of go get to build and install packages is deprecated (In earlier versions of Go, 'go get' was used to build and install packages. Now, 'go get' is dedicated to adjusting dependencies in go.mod.)
All you need should be go install:
go install github.com/githubnemo/CompileDaemon#latest
That should generate a CompileDaemon executable in your directory named by the GOBIN environment variable.
But you need $GOBIN itself in your $PATH (from your ~/.zshrc for instance):
export PATH="$PATH:$GOBIN"

Unable to run gomobile init or version on mac

I am a go newbie. I am trying to install gomobile for evaluation on MacOS BigSur but can't get past the init step.
running go version returns go version go1.17.6 darwin/amd64
Following the directions, the gomobile install appears to work properly (after explicitly adding gomobile to my PATH), but running init or version fails.
gomobile: go install golang.org/x/mobile/cmd/gobind failed: exit status 1
go install: version is required when current directory is not in a module
Try 'go install golang.org/x/mobile/cmd/gobind#latest' to install the latest version
gomobile version unknown: cannot test gomobile binary: exit status 1, no required module provides package golang.org/x/mobile/cmd/gomobile: go.mod file not found in current directory or any parent directory; see 'go help modules'
I'm guessing this has something to do with environment variables but any suggestions and/or help would be appreciated.
I did manage to finally get gomobile set up, but the process was painful and the official "experimental" documentation was incomplete and not at all helpful. Here's the steps that worked for me on MacOS (BigSur):
Follow steps to install GO from website
Install gomobile
go install golang.org/x/mobile/cmd/gomobile#latest
Install Xcode command line tools. If installed but not found, you may need to run the following:
xcode-select -r
Install Android NDK. This can be done via Android Studio’s Tools->SDK Manager. Need to ensure that the installed version is supported by gomobile since the latest version of the NDK is NOT supported by gomobile.
Update shell to include the following exports:
export PATH=$PATH:/Users/mikem/go/bin
export ANDROID_HOME=/Users/mikem/Library/Android/sdk
export ANDROID_NDK_HOME=/Users/mikem/Library/Android/sdk/ndk/23.1.7779620
Create a working directory
mkdir myworkdir
Create a module directory
mkdir mymodule
cd mymodule
Create a .go file in that module directory and give it some content. Note that the package name should be different than the module name or the bind may complain that it can't find anything
package mymodulelib
import "fmt"
func SayHello() {
fmt.Println("Hello from mymodule")
}
From the work directory, initialize the module
go mod init mymodule
Install gobind - it will complain that you should be using the go install method instead of the go get (deprecated) but the go install does not work (at least for me)
go get golang.org/x/mobile/cmd/gobind
Go back into the module directory, initialize and then generate iOS and android code
cd mymodule
gomobile init
gomobile bind -target ios
gomobile bind -target android

GoLand IDE not correctly compiling. How to setup correct paths?

I am trying to setup GoLand correctly to be able to use it with Go.
I'm trying to run the follow simple HelloWorld go project.
package HelloWorldProject
import "fmt"
func main(){
fmt.Printf("Hello")
fmt.Printf("1+1 = ", 1+1)
}
This is my console's results:
GOROOT=/usr/local/Cellar/go/1.10/libexec #gosetup
GOPATH=/Users/jeanmac/go #gosetup
/usr/local/Cellar/go/1.10/libexec/bin/go build -i -o /private/var/folders/r5/rfwd1cqd4kv8cmh5gh_qxpvm0000gn/T/___Hello /Users/jeanmac/go/src/github.com/jpere547/HelloWorldProject/Hello.go #gosetup
Compilation finished with exit code 0
I am on Mac OS and I installed Go using Brew.
Results of brew info go:
go: stable 1.10 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.10 (8,150 files, 336.9MB) *
Poured from bottle on 2018-03-22 at 19:38:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Required: macOS >= 10.8 ✔
==> Options
--without-cgo
Build without cgo (also disables race detector)
--without-race
Build without race detector
--HEAD
Install HEAD version
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
The GoLand configurations is below:
GOROOT
GOPATH
It looks like you are trying to run a non main package. Specifically, instead of package HelloWorldProject, you should use package main. After that the IDE will be able to not only build but also run the package.
When you do go build something.go or go isntall something.go that only builds/installs the go package to give you an executable. You will need to run that executable.
The easiest way to run simple Golang programs is simply use go run something.go that will run your go file.
As long as your GOPATH is correctly set it should work
you can try to change the package name instead of main
There is an option in configuration to run after build. I think you are missing to set it's configuration.Steps:
1. Go to Run
2. Select "Edit configuration"
3. Select the check-box below output directory field specifying "Run after build".

Golang zmq binding, ZMQ4, returns package error not finding file zmq.h

I am trying to include ZMQ sockets in a Go app but both zmq4 and gozmq (the referred ZMQ binding libraries for Go) are giving me problems. I would like to understand why zmq4 specifically isn't importable on my system.
I am running a Windows 8 system and I used the windows installer from the ZMQ website for version 4.0.3. I am primarily concerned about getting zmq4 set up and here is the result of my "go get" query on the github library's location:
> go get github.com/pebbe/zmq4
# github.com/pebbe/zmq4
polling.go:4:17: fatal error: zmq.h: No such file or directory
compilation terminated.
This issue is not alleviated by cloning the Github repository - the error remains the same.
I know the issue has to do with the C library zmq.h that is located in the "include" folder of my ZMQ installation, but whether the dependency is held up by a pathing issue or an external tool issue is a mystery to me.
A similar error has come up in regards to node.js and is the solution I see others referred to, outside of node scripting, but it was unsuccessful in my case.
I've so far included the path to the "include" folder in my PATH environment variable and previously placed zmq.h inside of the zmq4 top-level folder. I don't have much of an arsenal otherwise to understand this problem because I am new to C and C-importing packages in Go
I wanted to do the same thing, but on Windows 7, and here is what I had to do.
Since the Go bindings are using cgo to integrate with zeromq, you need zeromq built with gcc. There are no pre-built binaries, so you'll have to build them yourself, with mingw or similar, but this process is easier than it may sound, and nicely described on the zeromq site.
As #photoionized pointed out, C_INCLUDE_PATH and LIBRARY_PATH need to be set when building the Go bindings.
(In my case, I ran into a problem when compiling libzmq with IN6_ADDR not being defined. The only solution I found was, inspired by this issue, to manually add the line #include <in6addr.h> to the windows.hpp file.)
The Windows installer version of ZeroMQ won't work with zmq4, you need to compile from source with gcc, I recommend using MSYS2.
Install and update MSYS2 following the instructions from
http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/
Start the mingw32_shell.bat or mingw64_shell.bat based on Go arch (32bit or 64bit)
pacman -S mingw-w64-(x86_64|i686)-toolchain make (x86_64 for 64bit, i686 for 32bit)
cd into zeromq src folder (C:\ path starts with /c/ inside the shell)
./configure
make
make install
CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go get github.com/pebbe/zmq4
Copy the following dlls and put them next to your go program (.exe):
/usr/local/bin/libzmq.dll
/mingw(32|64)/bin/libgcc*.dll
/mingw(32|64)/bin/libwinpthread*.dll
/mingw(32|64)/bin/libstdc++*.dll
Here's updated steps for #user2172816's MSYS2 solution:
Install and update MSYS2 following the instructions from http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/
Start the mingw32_shell.bat or mingw64_shell.bat based on Go arch (32bit or 64bit)
pacman -S mingw-w64-(x86_64|i686)-toolchain make (x86_64 for 64bit, i686 for 32bit)
Add C:\msys64\mingw64\bin to your Path (pkg-config is there)
Restart the msys2 shell to get the new Path
Download and unzip libsodium source: https://github.com/jedisct1/libsodium/releases
cd into libsodium folder (C:\ path starts with /c/ inside the shell)
./configure --build=(x86_64|i686)-w64-mingw32
make
make install
Add /usr/local/lib to PKG_CONFIG_PATH (export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig)
cd into zeromq src folder
./configure --build=(x86_64|i686)-w64-mingw32
Add
#ifdef ZMQ_HAVE_MINGW32
#include <winsock2.h>
#include <windows.h>
#include "netioapi.h"
#endif
To the top of src/tcpaddress.cpp
make
make install
CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go get github.com/pebbe/zmq4
CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go build in your project directory
Copy the following dlls and put them next to your go program (.exe):
/usr/local/bin/libzmq.dll
/mingw(32|64)/bin/libgcc*.dll
/mingw(32|64)/bin/libwinpthread-*.dll
/mingw(32|64)/bin/libstdc++*.dll
/usr/local/bin/libsodium-*.dll
maybe? /usr/local/bin/libsodium-*.def
An updated answer using MSYS2.
Install MSYS2 MSYS2 installation guide.
Make sure to choose the correct installation 32bit or 64bit.
Open the appropriate shell MSYS2 MinGW 64-bit or MSYS2 MinGW 32-bit. All further steps assume you are using this shell.
Update packages following instructions at the installation guide.
Install libtool pacman -Sy libtool.
Download zmq source code to a location of your choice.
Navigate to the zmq source folder.
To generate the configure file, run the autogen tool by running ./autogen.sh.
In the probable case that step 8 fails:
Find the file at fault (probably version.sh).
Replace line endings by (replace file by the actual filename).
cp file file.bak
tr -d '\r' <file.bak> file
If this fails you'll have to dive in the code and find the problem.
Run the configure tool ./configure.
In the probable case of failure. Comment out empty else clauses in the configure file.
Add Go to Path: PATH=${PATH}:<go bin directory>.
Install Go Package: CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go get github.com/pebbe/zmq4
To install ZMQ in windows: Problem in Installing Golang ZMQ for windows - fatal error: czmq.h: No such file or directory
First of all, install the msys64. Download the software from https://www.msys2.org/ and install it on C:\msys64.
Then add C:\msys64\mingw64\bin to PATH environment variable of the windows.
Then run the following commands (in CMD) one by one.
pacman -Su
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
pacman -S base-devel gcc vim cmake
pacman -S mingw-w64-x86_64-libsodium
pacman -S mingw-w64-x86_64-zeromq
Finally, run the Go install command:
go get github.com/pebbe/zmq4
Finished.

How to properly install Cabal 1.18.02 in Windows 7?

I'm trying to install the new Cabal in Windows 7. It successfully installs 1.18.02, but when I run "cabal --version" it shows 1.16. The folder C:\Users\me\AppData\Roaming\cabal\bin is at the front of my PATH.
When I directly run the 1.18 executable, I get a bunch of "cabal.exe does not exist" errors.
How can I configure Cabal to use version 1.18 by default?
There is a path conflict due the way haskell platform installs cabal.exe
As seen in this ticket
To fix it:
Delete `C:/program files(x86)/Haskell platform/lib/extralibs/bin/cabal.exe.`
In any case, you can use search tool to find which cabal.exe to delete.
I used cabal-dev to install cabal 1.18 to a sandbox, such that C:\cabal-dev\cabal-dev\bin contains the latest (1.18) cabal executable. I then set C:\Cabal-dev\cabal-dev\bin as the first Variable value in the "Path" variable in "systems variables" (as opposed to the "PATH" variable in "user variables"). That worked for me, and allowed me to keep the older build of cabal intact, (in case its needed later).
It worked for me. Good luck. Here are my exact steps:
Install cabal-dev
open command prompt anywhere
execute following command: “cabal install cabal-dev”
Install new cabal
create directory: c:\cabal-dev, cd to it
execute following command: “cabal-dev install cabal-install”
Set new, upgraded cabal path as higher priority
Close any open command prompts
run: Sysdm.cpl
Go to the “Advanced” tab
Click “Environment Variables”
Select the “Path” variable listed in the “System variables” (lower) windows
Click edit
Add following string to the beginning of the variable value:
“C:\Cabal-dev\cabal-dev\bin;”
Ok, Ok, Ok
Ensure latest cabal is being run:
Open command prompt (anywhere except c:\cabal-dev\ *)
execute following command: “cabal –V”
cabal version should be 1.18.0.2
One other possible cause of this error (aside from the cabal in extralibs problem described in another answer) is that the ld in your path can't read the .o files that GHC produces.
I hit this problem with GHC 7.8.1 RC1, and could see this going wrong by running cabal install with -v3. The 'ld' on my path was coming from Haskell Platform 2013.2.0.0.
Adding mingw\bin from the new GHC's directory to the path fixed the problem.

Resources