Go - "version" package and How it works? - go

I am researching approaches to organize projects in different open source projects (for example operator-framework/operator-sdk) and sometime find version package with version.go file:
├── build
├── cmd
├── deploy
├── go.mod
├── go.sum
├── pkg
├── tools.go
└── version
└── version.go
package version
var (
Version = "0.0.1"
)
I tried to find how to use it to control application version, but found only approach uses -ldflags.
Are exist other ways to use version package and version.go file?

Are exists other ways to use version package and version.go file?
No.

Related

gRPC modules not being imported in my project

I'm creating a simple gRPC server in Go and it has the following structure
├── go.mod
├── go.sum
├── main.go
└── proto
└── consignment
├── consignment.pb.go
└── consignment.proto
for some reason, some of the packages I'm using aren't being imported in my app, and when I hover one of them, my IDE says:
"google.golang.org/grpc/reflection" in any of
/usr/local/go/src/google.golang.org/grpc/reflection (from $GOROOT)
/home/gabriel/go/src/google.golang.org/grpc/reflection (from $GOPATH))
I already tried to run lots of commands like:
go mod tidy
go get -u
but I cannot solve the problem.
I'm pretty new with golang and go modules, and I'm hoping that someone can help me out :)
Updating VSCode to the latest version solved the problem

Is there a way to run a Go module from another directory

I have the following project structure, outside of GOPATH.
. // Project root
├── Dockerfile
├── .env
├── README.md
└── src
├── main.go
├── go.mod
├── go.sum
├── internal
│   ├── somepackage
│   │   ├── main.go
│   │   └── types.go
│   ├── someother
│   │   ├── main.go
│   │   ├── oauth.go
│   │   └── types.go
│   └── models
│   └── main.go
└── pkg
   ├── somepackage
   │   └── main.go
   └── anotherpackage
   └── main.go
I want to run my Go module code located in the src directory.
When I cd into the src directory and go run . or go build . my code, it works perfectly.
When I stand at the root of my project, I am unable to run go run ./src or go build ./src. I get the following error.
src/service.go:8:2: cannot find package "web-service/internal/auth" in any of:
/usr/lib/go/src/web-service/internal/auth (from $GOROOT)
/home/miloertas/Packages/go/src/web-service/internal/auth (from $GOPATH)
src/endpoints.go:3:8: cannot find package "web-service/internal/handlers" in any of:
/usr/lib/go/src/web-service/internal/handlers (from $GOROOT)
/home/miloertas/Packages/go/src/web-service/internal/handlers (from $GOPATH)
It's important that my source code remains in this src directory.
It is equally important that I am able to run and build my code from the root of my project (For example the .env file is located at the root of the repository).
I am therefore looking for a way to run or build my code in the src directory from the root of my project.
I tried moving the go.mod at the root of the project and running and ran go run ./src but this causes issues of its own:
The go command is now unable to locate all the sub-packages in internal and pkg
VSCode is now lost and executing tests is impossible for some reasons (Mainly because all sub-packages are not found).
Since Go 1.18, it's now possible to achieve this with Go workspaces.
Using the following directory structure
parent-dir/
└─ go.work
hello-world/
├─ go.mod
└─ main.go
You can run the hello-world module from the parent-dir using go run hello-world.
go.work
go 1.18
use ./hello-world
go.mod
module hello-world
go 1.18
Note: it is possible, not recommended as pointed out by #Volker
It's important that my source code remains in this src directory. It is equally important that I am able to run and build my code from the root of my project (For example the .env file is located at the root of the repository).
These two requirements are contradictory. You have to let go of one.
Especially the second one is unfounded: Do not use go run, use go build. Make the path to look for the .env file a command line option to your program (Go is not PHP or JavaScript, there simply is no project or source root for the executing binary). Or build the executable somewhere but execute it in you project root.
Note that having a src folder is -- to put it mildly -- uncommon.
I tried moving the go.mod at the root of the project and running and ran go run ./src but this causes issues of its own:
Well, start by not using go run at all, use go build. And then try building the actual main package. All the go tooling works best on packages, not on file system folders. If your module is named playing.hardball/for-unspecific-reasons and package main is in src try go build playing.hardball/for-unspecific-reasons/src.
Takeaways even if this doesn't work out the way you want:
Do not use go run. The reasons are manyfold, it is useful to run single file scripts and a loaded footgun for basically every other use case.
The go tool works on import paths. In simple cases the import path can be inferred from the filesystem.
A compiled executable has no notion of a "project directory", "source", "classpath" or whatever, it is a standalone executable runnable everywhere and completely detached from its sources.
Make all filesystem lookup path a configuration option (cmdline flag or environment variable); provide practical defaults (e.g. ./); use that when running your executable to announce where to find static stuff like .env files, templates, icons, css files, etc.

Error building Go project with /cmd structure (multiple entry points)

Here is the directory structure of my project (~/go/src/bitbucket.org/a/b):
├── cmd
│   ├── c
│   │   └── main.go
│   └── d
│   └── main.go
├── config
│   ├── config.go
│   ├── default.json
│   └── development.json
├── go.mod
├── go.sum
├── log
│   └── log.go
├── main.go
I need to compile 2 binaries (one for each module in cmd/).
I have tried running GO111MODULE=on go build ./cmd/c from project root (~/go/src/bitbucket.org/a/b). It silently finishes without doing anything.
I also tried running GO111MODULE=on go build -o test ./cmd/c. It created 29kb test file. When i add execution rights to it and run, it finishes with error:
./test: 2: ./test: Syntax error: newline unexpected
I have tried using go 1.12.5 and go 1.11.10.
Also when i put main.go file from any of the cmd directories to project root directory and build, the compiler builds it just fine (binary file size is ~33mb).
Is it possible to use 2 compiler entry points in a single project?
You can use go install ./... and it will create the executables in $GOPATH/bin directory if you are looking to get the executables
And regarding the multiple compiler entry points, you can. You can build using go build ./cmd/c ./cmd/d . but you cannot get the executables as per the GO Documentation -o can be only used in the case of single package. Instead you can write a makefile to get all the executables with a single make target.
And regarding the error that you are seeing, I would need more information. When I tried to build a sample application, everything works fine. I am not using GO111MODULE flag though.
go build gives me stackoverflow executable
go build -o test ./cmd/c gives me test executable
go build ./cmd/c gives me c executable
For your convenience I uploaded the project to github repo

vendor directory not being used to resolve imports on go build

I'm in the process of relearning Go. I installed the latest Go version (1.7.1) using gvm and I am looking to build a simple rest api app using gin. I installed it using glide get https://github.com/gin-gonic/gin (glide) and that created a "vendor" folder on my project root. Running my app though, go run main.go, I encounter this error
main.go:3:8: cannot find package "github.com/gin-gonic/gin" in any of:
/home/yowmamasita/.gvm/gos/go1.6.3/src/github.com/gin-gonic/gin (from $GOROOT)
/home/yowmamasita/.gvm/pkgsets/go1.6.3/global/src/github.com/gin-gonic/gin (from $GOPATH)
It is not resolving the "vendor" directory glide just created
.
├── glide.lock
├── glide.yaml
├── main.go
├── README.md
└── vendor
└── github.com
└── gin-gonic
└── gin
Not sure what's happening here, I thought after 1.5, it should be able to resolve imports from "vendor" directories without doing anything. I even added my projects folder on my $GOPATH
/home/yowmamasita/.gvm/pkgsets/go1.7.1/global:/home/yowmamasita/goprojects
What am I doing wrong here? I tried 1.6.3 too and I get the same error.
Please make sure:
Add the workspace (/home/yowmamasita/goprojects) to $GOPATH variable.
Typically under workspace there will be three directories which are bin, pkg and src. More details
You can omit pkg and bin, but the project which is using vendor packages or your custom package must be placed under $GOPATH/src, otherwise go compiler will not recognized it.
More discussions can be found here and here
The structure should look like:
$GOPATH
└── src
└── YOURPROJECT1
├── source codes #1
└── vendor/
└── YOURPROJECT2
├── source codes #2
└── vendor/

Xamarin test Cloud incompatibility issue

I am running the test scripts for my app and I am getting the following error.
Xamarin.UITest.dll version (1.3.8.0) and test-cloud.exe version (1.0.0.0) are incompatible
Any advise ?
There is a matching version test-cloud.exe within each Xamarin.UITest.XXXXX Nuget package and that is the .exe that you should use to run your tests with.
Example:
/packages/Xamarin.UITest.1.3.8.1491-dev
.
├── Xamarin.UITest-License.rtf
├── Xamarin.UITest.1.3.8.1491-dev.nupkg
├── lib
│   ├── Xamarin.UITest.dll
│   └── Xamarin.UITest.xml
└── tools
└── test-cloud.exe
/packages/Xamarin.UITest.1.3.8
.
├── Xamarin.UITest-License.rtf
├── Xamarin.UITest.1.3.8.nupkg
├── lib
│   ├── Xamarin.UITest.dll
│   └── Xamarin.UITest.xml
└── tools
└── test-cloud.exe
I observed one thing.
The package folder for my solution has Xamarin.UITest of 1.0.0.0 and my UITest project had 1.3.8 version. Can you tell me that how to push it to github ?

Resources