Running GUI apps in Goland IDE - user-interface

When I build in Terminal, I can use a flag to say I want to build for GUI:
go build -ldflags="-H windowsgui"
However, I just started using JetBrains Goland and I don't how to run GUI apps. What can I do?

go build will only build the application.
To actually run the application, you should go to Run | Edit Configurations... | + | Go Application and configure the application as you need.
Here you will need to set two options:
add -ldflags="-H windowsgui" to the Go tool arguments option
configure the Output directory to be in the same directory as your .manifest file
Setting the output directory is critical in order to run the the application without encountering the following panic panic: TTM_ADDTOOL failed described in this issue.
Then you can run the configuration via Run | Run... and select the configuration you've just created.

Related

Can't run go tests from GoLand (Intellij Idea): compilation failed

I've made simple application in go and trying to run tests with GUI tools of GoLand.
In myfile_test.go file near test func I press green button that should start test. But I get error message: "Compilation failed" and message in console:
# command-line-arguments [command-line-arguments.test]
./myfile_test.go:21:11: undefined: MyStruct
./myfile_test.go:22:12: undefined: MyFuncName
./myfile_test.go:33:12: undefined: AnotherStruct
Compilation finished with exit code 2
Other variants (Run test with Coverage/CPU Profile) don't work either. GoLand 2020.1 EAP. The same problem occurred in older versions of GoLand.
But test from console starts normally:
go test -v
=== RUN TestMyStruct_MyMethod
--- PASS: TestMyStruct_MyMethod (0.00s)
PASS
ok _/home/username/projects/my_project_name 0.002s
The answer from #porfirion worked for me.
Basically, you need to tick "Enable Go Modules integration" in GoLand under "Preferences" -> "Go" -> "Go Modules"
Goland will then reindex your project and this can take a long time if you have a large project with many modules. Mine took 30 mins. You can check the reindexing progress status at the bottom of the IDE.
The test run will work after the reindexing.
I solved the problem with initializing new go module and enabling Go Modules Integration:
1) Run in Terminal go mod init my_module_name
2) Click "File -> Settings" or press Ctrl+Alt+S
3) Check "Enable Go Modules Integration" and Apply button
Now all test functionality in GoLand works well (including tests with coverage, etc.)
The issue here is that you're pointing to a single file. All other files, even though they're in the package, aren't going to be imported. go build has the same behaviour, if you are targeting a single file to build and forget to include other files it uses in the command.
Change the test kind to "Directory" and files to "/project-root-dir". This will then include all files in the directory during build and should look for all _test.go files to execute tests.

IntelijIDEA (Goland) uses /private/var/folders/

My Goland's runner (run/debug configurations) uses /private/var/folders/7b/50mzg8x17q55rxfg3b0kpj88xcm2lx/T in os.Args[0]. I know that os.Args[0] is the path to my program, but can I do os.Args[0] to be my working directory in Goland runner?
P.S. Working directory in Edit configurations set properly
I know that os.Args[0] is the path to my program, but can I do os.Args[0] to be my working directory in Goland runner?
No, you cannot make os.Args[0] to be the working directory, since as you correctly identified, it's used to locate your program on disk.
There are two changes you can make:
change your code to stop relying on os.Args[0] and start using os.Getwd() to get the working directory. Then you can edit your Run Configuration via Run | Edit Configurations... | <name of the run configuration> | Working Directory parameter.
if you really want you binary to be in a certain directory, then you can go to Run | Edit Configurations... | <name of the run configuration> | Output directory to have the IDE run the compiler and place the executable wherever you need to.
If you need to make these changes for all future Run Configurations, then edit Run | Edit Configurations... | Templates | Go Build, or Go Test and change either the Working Directory or the Output Directory fields. Existing Run Configurations will not be changed.

How to run/debug a beego app using Gogland (go language)

Im using Gogland (IDE from JetBrains. Version 1.0 Preview/EAP Feb, 10 2017) to create a Beego web app.
I can run it from command line with:
bee run
and everything works.
However if I run it from the IDE with the following configuration
when I go to localhost:8080 it says it can not find the template file in path:
I thought it was related to GOPATH, but then I realized that Gogland IDE is probably running
go run main.go
instead of
bee go
and when I checked runing
go run main.go
from the shell, I got the same issue: cant find the template.
I even tried to run the 'bee' command from the IDE. I partially succeed. With this configuration:
I can run it from the IDE, but the debugger doesn't stop in any breakpoint. IE: I can only run (but not debug) it, from Gogland.
So my question is how to make gogland IDE debug a beego project
Use Delve and Remote Debugging Configuration(start from gogland eap9).
Run your bee app like:
bee dlv -package="app_name" -port=2345
On gogland make configuration Go Remote with port which you set previous.
You cannot change the IDE to run the bee command. However, you can change the run configuration to a Go Application by going to Run | Edit Configurations | + | Go Application select the package type then type the full package name (github.com/dlsniper/demo for example if your package main is under GOPATH/src/github.com/dlsniper/demo) and make sure the working directory is where you need it to be. Hope it helps.
I resolve this by setting:
beego.BConfig.WebConfig.ViewsPath="/Users/xxxxx/go/src/xxxxxxx/views" // your views directory
by the way: if you have staticpath , you should also use absolute path.
eg: beego.SetStaticPath("/static", "/Users/xxx/go/src/xxxx/static")
it's only used when debug, if you publish your program, you should remove this hard code config.

Debugging revel with IntelliJ

How can we configure IntelliJ to debug revel apps?
In Run/Debug Configurations, it's not at all obvious what to enter for File, Go tool arguments, and and Program arguments
Create your project, for this example i will be using canonical revel new github.com/myaccount/my-app
Run in terminal revel run github.com/myaccount/my-app to generate tmp/main.go - this file is needed by intellij
Select "Edit configuration" to create Run configuration. Select Go Build
Select YOUR file "main.go"
Paste in "Program arguments" string -importPath github.com\myaccount\my-app
Click on "Before launch: ..." and create "Run external tool"
There: Program: <your gopath>/bin/revel(Windows - revel.exe) Paramerets: build github.com/myaccount/my-app, Working dir <your gopath>/bin
Screenshot Example Config
Now You can Run and Debug in panel IntelliJ

Recommended Go build system for a CI server?

So I have a go project with go-gettable dependencies, tests, etc.
I want to integrate it into Jenkins. Is there an automated build system for go that anyone recommends for this use case, other than writing makefiles?
I need:
automatic installation of go-get deps (they can be in a spec file of course)
recursive build.
running tests.
GOPATH/GOROOT management (to isolate SDKs and paths)
I've used godag in the past for this sort of job but it seems a bit unmaintained.
EDIT: For the time being I'm living with the following script entered directly into Jenkins as a build step:
#this gets the dependencies but doesn't install them, avoiding permission problems
go get -d
#build the packages, -x outputs the compiler command line
go build -x
#this was tricky - ./... means "for each sub-package recursively"
go test ./...
You can do it with teamcity as well.
Here is an example for building terraform.
Teamcity agent setup:
Install Go
Add Go to path
Don't forget to restart agent as path was changed
Teamcity build setup:
Use agent side checkout (if we want to include the .git folder, which a lot of build scripts make use of)
Use checkout rule (we want to use the Go convention):
+:. => src/github.com/mitchellh/terraform
Build steps:
echo cd %system.teamcity.build.checkoutDir%
cd "%system.teamcity.build.checkoutDir%"
path=C:\Program Files (x86)\Git\bin;%env.Path%;%system.teamcity.build.checkoutDir%\bin;
echo path=C:\Program Files (x86)\Git\bin;%env.Path%;%system.teamcity.build.checkoutDir%\bin;
set GOPATH=%system.teamcity.build.checkoutDir%
echo set GOPATH=%system.teamcity.build.checkoutDir%
echo "Getting dependancies"
go get golang.org/x/tools/cmd/vet
go get golang.org/x/tools/cmd/cover
go get golang.org/x/tools/cmd/stringer
go get github.com/mitchellh/gox
echo cd %system.teamcity.build.checkoutDir%\src\github.com\mitchellh\terraform
cd "%system.teamcity.build.checkoutDir%\src\github.com\mitchellh\terraform"
echo "Update resources"
go get ./...
echo "Run static code analysis"
go tool vet -asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr .
echo "Build"
cd scripts
sh build.sh
echo "Run unit tests"
go test -timeout=30s -parallel=4
echo "Run code coverage"
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
rm coverage.out
I am using a Team City build server on a Mac that runs a rake file, in the rake file I do all the commands to get dependancies, (go gets), tests and builds to the correct environment.
let me know if you want any pointers in writing the Rake files,
As a side note, i have been creating functional tests for REST Api's using this frame work. This has saved my code many times over. http://github.com/DigitalInnovation/cucumber_rest_api
Since August 2019, TeamCity 2019.1 now supports Go directly.
See "Building Go programs in TeamCity"
To enable Go support in TeamCity,
go to Build Configuration Settings | Build Features,
click Add build feature, and
select Golang from the list.
Support for Go is provided by TeamCity out of the box, there are no external plugins required.
TeamCity parses results of go test command execution. The results are persisted and it is possible to review the figures in a historical perspective.
Consequently, all the TeamCity features that are related to test reporting are now available for Go developers.

Resources