Beego: Routing issues with modules - go

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.

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.

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.

Set Go variable with ldflags conflicts with vendor folder

I'm currently developing a small Go app and I want to set a specific variable (like Version, GitCommit, BuildID...etc.) at build or runtime (with go build or go run) by using the -ldflags option.
Because in my company we have several projects with the same base, I decided to extract the code with these variables in a separate "info" module which could be imported in every project.
Here's my problem, say I'm running the app like this:
go run -ldflags "-X git.mycompany.com/utils/info.Version=1.0.0" app.go
This works well, and the variable is set correctly even though the variable is not part of the "main" app but in a dependency.
Then I decided to deploy the app so I used the new dep tool to generate the vendor folder.
Therefore, the "info" dependency is now in: vendor/git.mycompany.com/utils/info
Now when I run the same command as above, the said variable (Version) is not set anymore.
Am I missing something here ?
As soon as I delete the vendor folder, everything works fine again. It's like this vendor folder is conflicting with the -ldflags option or something.
Thanks in advance!
We had exactly the same problem, after lots of research we stumbled upon the solution in a comment to GitHub issue: cmd/link: -X doesn't work for vendored packages.
Solution: the full path name, relative to $GOPATH should be specified.
It works when developing git.mycompany.com/utils/info because the full path is correct.
It doesn't work for vendored dependencies because the full path from $GOPATH would be like git.mycompany.com/name-of/package/vendor/git.mycompany.com/utils/info.Version=1.0.0
Unfortunately, no documentation seem to be present about this ( for further info look at the issue ) but as Dave Cheney points out in a comment:
this is a side effect of the language way vendoring is implemented

Modifying an imported library in Go

My Problem
Elastic Beats is an open source project for log shippers written in Go. It features several log outputs, including console, Elasticsearch and Redis. I would like to add an output of my own - to AWS Kinesis.
I have cloned the repo to ~/github/beats, and tried building it:
$ cd filebeat; go build main.go
However, it failed due to a missing library which is a part of the project:
main.go:6:2: cannot find package "github.com/elastic/beats/filebeat/cmd" in any of:
/usr/local/go/src/github.com/elastic/beats/filebeat/cmd (from $GOROOT)
/Users/adam/go/src/github.com/elastic/beats/filebeat/cmd (from $GOPATH)
A directory of the project is dependent on a package from the same repo, but instead of looking one directory up the hierarchy it looks in the GOPATH.
So, go get github.com/elastic/beats/filebeat/cmd fetched the code, and now go build main.go works. Changing the code in my GOPATH is reflected in these builds.
This leaves me with an structural inconvenience. Some of my code is at a working directory, and some of it is at my GOPATH and included by the code in my working directory.
I would like to have all my code in a single directory for various reasons, not the least being keeping everything under version control.
What Have I Tried
Mostly searching for the problem. I am quite new to Go, so I might have missed the correct terminology.
My Question
What is the right way to edit the code of an imported library in Go?
One of the recommended ways to work with other's packages is:
Get the sources of the original package:
go get github.com/elastic/beats
As a result you will clone project's git repository to the folder
$GOPATH/src/github.com/elastic/beats
Make some fixes, compile code, fix, compile... When you make go install package will be compiled and installed to your system. When you need merge updates from original repository you can git pull them.
Everything is OK. What's next? How to share your work with others?
Fork project on github, suppose it will be github.com/username/beats
Add this fork as another remote mycopy (or any other name you like) to your local repository
git remote add mycopy git://github.com/username/beats.git
When all is done you can push updated sources to your repo on github
git push mycopy
and then open a pull-request to original sources. This way you can share your work with others. And keep your changes in sync with mainstream.
Previous answers to this question are obsolete when developing projects that using Go Modules.
For projects that using Go Modules, one may use the following command to replace an imported library(eg. example.com/imported/module) with a local module(eg. ./local/module):
go mod edit -replace=example.com/imported/module=./local/module
Or by adding the following line into the go.mod file:
replace example.com/imported/module => ./local/module
Reference Docs: https://golang.org/doc/modules/managing-dependencies#unpublished
A project working copy should be checked out into $GOPATH/src/package/import/path - for example, this project should be checked out into /Users/adam/go/src/github.com/elastic/beats. With the project in the correct location, the go tooling will be able to operate on it normally; otherwise, it will not be able to resolve imports correctly. See go help gopath for more info.

Using LiteIDE and Google App Engine - Folder Structure

I have a tool, call it mytool, that makes use of other code in a separate package I have written - call it mypackage.
I can build and test it in LiteIDE if I use the following structure:
/src/mypackage/mypackage.go
/src/mytool/tool.go
/src/mytool/tool.yaml
but when I want to test or deploy on GAE using:
cd ... \src\
\go\gae\go_appengine\dev_appserver.py mytool/
or
\go\gae\go_appengine\appcfg.py update mytool/
I have to move stuff around to:
/src/mytool/mypackage/mypackage.go
/src/mytool/tool.go
/src/mytool/tool.yaml
Is there something I can change in LteIDE so it can handle the second structure or is there something I can change in the GAE setup to make it handle the first structure?
I am working on Windows 7.
Have you tried installing the package using go install? That creates the compiled binary in the packages directory so the rest of your packages/code can treat it the same as a 3rd-party package.

Resources