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

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.

Related

Can't run test function in GoLand: cannot find package "."

I have used VS Code as my code editor for building service using Go for almost one year. Then, I tried to switch to GoLand. But, when I tried run a test function there is an error: cannot find package "." What is the problem?
Note: I use go module as go dependency management tool. When I use dep (in another project), there is no error when running a test function. My project is in GOPATH.
Please make sure there's a valid go.mod file defined at the ROOT of your project i.e. in $GOPATH/src/<Project-name>.
If you don't have, you can create one using go mod init command using the shell. More information on the same - https://github.com/golang/go/wiki/Modules
After that please try running the test from the shell. First cd into the directory where the test file is present. Then, use go test . -test "<TestName>" -v to run your test. If the issue goes away, you can run the test from IDE and it should work fine.

Beego: Routing issues with modules

Routing doesn't work at all for me in bee if I use modules while GOPATH old approach works perfectly.
I am new in golang, correct me if I did something wrong.
I have been trying to create API project with bee, but I found that all new projects have to use the modules approach released in go 1.11.
I used bee api api-name command to scaffold the new project. It appeared in GOPATH. I moved out it from GOPATH path to another directory. Then I did go mod init mod, then I was able to run bee run and API was successfully started but routing didn't work. I did some comparing and found out that commentsRouter file wasn't generated.
What am I do wrong?
I tried to do set GO111MODULE=on and then go get -u github.com/beego/bee but this wasn't helped as well.
Now I can't even run bee API, I see
0004 Failed to build the application: main.go:4:2: package api-name/routers is not in GOROOT (c:\go\src\api-name\routers)
What's happens? I am confused. Official go documentation says that I should prefer modules approach with new projects but I can't find somewhere in docs anything about how to scaffold and use bee with modules system.
Just because missing file commentsRouter_controllers.go. Don't know how beego does not generate file routers/commentsRouter_controllers.go when running app outside $gopath.
Then we can only access to localhost/swagger. Any another path (or route) is all 404 returned.
Solution:
Not a good way but worked:
copy source code of $your_project to $gopath/src
cd $gopath/src/$your_project > bee run (just for generating file commentsRouter_controllers.go) > stop running app
copy file $gopath/src/$your_project/routers/commentsRouter_controllers.go to $your_project/routers
cd $your_project > bee run
Good luck
It looks like latest Beego Bee supports generating routes outside the GOPATH.
Have a check. It did work for me now.

How to proper configure "Go Build" and what are the differences between Run Kind

I am new to Go language and also to the IDE GoLand so I am sorry if this is very basic.
I am currently trying to configure Run Kind for package for all my files, the problem is I cannot seem to get the configuration straight, I get this error:
"can't load package: package Course: unknown import path "Course": cannot find module providing package Course"
My GOROOT is the standard in C: and GOPATH is in my directory of workplace with the folders: scr, bin and pkg. Inside scr is course folder with training files for Go.
I have tried to google every option on how to properly configure the go Build configuration, I might be missing to install packages not sure to be honest, I have installed the gotools and everything from golang.org, and tried following the guide from GoLand in JetBrains but no luck in properly configuring package, or Dir options.
I can create a go Build for each file using Run Kind: File, but I want to create one for all the files inside the folder not one each time I want to run one.
Also I have no idea what -i in Go tool arguments means.
I believe what you need to do is simply enable go module integration. Find the setting at File | Settings | Go | Go Modules (vgo). In that panel also make sure Vgo executable is set to your Project SDK. If it's still not working, enabling the vendor experiment option at File | Settings | Go | Build Tags & Vendoring may help. Be sure your project specific settings aren't overriding these values as well.

Running GUI apps in Goland IDE

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.

How to run the whole project in GoLand?

I have a small project with several .go files. When I run main.go only this compiles but nothing else, so my application crushes. I understand that I have to change settings in Run -> Edit Configurations, but don't know what to do exactly. IDE also doesn’t see terminal pre-compiled package, so "Package" option instead of "File" doesn't work.
To run the whole project you have to go Run -> Edit Configuration, set Run Kind to Package and type in field Package your project directory name.

Resources