Some tips with Go and Gogland - go

Hi all. I'm very new with Go and Gogland. I have a project
I choose "Run kind" as Package - to run not only main file but a project. Why it cannot find main package??
How to import util.myprinter package to main.go to use it??
Please, help me

First, the general structure of your Go workspace seems to be wrong. You need to make it look more like this:
D:
|-- go_projects
| |-- bin
| |-- pkg
| |-- src
| | |-- FirstSteps
| | | |-- main.go
| | | +-- util
| | | +-- myprinter.go
| | |-- SecondProject
| | |-- ThirdProject
...
Second your import statement seems to be empty, I have no idea how GoLand works but if you want to use whatever is in your myprinter.go file, you will need to import the util package, assuming that the myprinter.go file declares its package as util at the top.
// FirstSteps/main.go
package main
import (
"FirstSteps/util"
)
func main() {
util.MyPrinterFunc()
}
And of course to be able to use anything from util there first must be something...
// FirstSteps/util/myprinter.go
package util
func MyPrinterFunc() {
// do stuff...
}
Edit: I'm sorry, I didn't actually answer your question initially. You're getting the error Cannot find package 'main' because of the wrong workspace setup I already mentioned. The Package path tells GoLand where the package you want to run is relative to the $GOPATH/src directory. So after you've setup your wrokspace correctly, you should set the Package path to FirstSteps since that package's absolute path will be $GOPATH/src/FirstSteps. If, later, you want to run the util package you would specify Package path as FirstSteps/util for GoLand to be able to find it.

Related

How to specify Dapr component locations with Sidekick?

I'm trying to get started with Sidekick for Dapr, and am having trouble telling Sidekick where the dapr components are.
By default it's going to %USERPROFILE%.dapr\components, but I'd rather it go to a folder local to the solution.
Looking at the code it appears that adding the following to the appsettings.json should work, but it isn't picked up.
"DaprSidekick": {
"RuntimeDirectory": "dapr",
"ComponentsDirectory": "C:\\Dev\\DaprPOC\\components",
}
However the components folder invariably becomes %USERPROFILE%\.dapr\components
Any help on how I specify the component locations with Sidekick?
When you set "RuntimeDirectory": "dapr" Sidekick will automatically look for component files in the dapr/components subdirectory in your solution. Try removing the ComponentsDirectory entry so it returns to defaults, and try a directory structure like this:
|-- MyProject
| |-- MyProject.csproj
| |-- dapr
| | |-- config.yaml
| | |-- components
| | | |-- my_component.yaml
The Dapr Sidecar should then load my_component.yaml.
You can also manually add the components directory in the dependency injection:
services.AddDaprSidekick(configuration, p => p.Sidecar =
new DaprSidecarOptions() { AppId = "daprservice", ComponentsDirectory = "C:\\Dev\\DaprPOC\\components" });

How to properly import a GO packge from Go Multi Module repo?

Can someone help me with the following:
I have 2 private git repositories private1 and private2.
I need to import a package from repo private1 into private2.
Structure of repo private1 is as follows :
private1 --
|
|- actions --
| | - go.sum
| | - go.mod (github.xyz.com/private1/actions)
| | - commons -- (package commons)
| | - commons.go
|-operations--
| | - go.sum
| | - go.mod (github.xyz.com/private1/actions)
| | - interceptor --
| | - interceptor.go
I want to import package 'commons' in my other repo private2.
What should be added to the go.mod of repo private 2?
if i use 'github.xyz.com/private1/actions' , i get the following error
go: github.xyz.com/private1/actions#v0.0.0-20211203184031-723259d523a2: unrecognized import path "github.xyz.com/private1/actions'": reading https://github.xyz.com/private1/actions?go-get=1: 404 Not Found
Since your modules/packages are in a private git repositories you cannot access them directly, you either have to download the modules locally and use them or publish them so that you (and others) can use.
How to guides:
Developing and publishing modules
Call your code from another module

malformed module path ... missing dot in first path element

I have a project with 2 different executables, each having it's own dependencies plus a shared dependency on the root, something like this:
Root
|->server
| |-> main.go
| |-> someOtherFiles.go
| |-> go.mod
| |-> go.sum
|->validator
| |-> main.go
| |-> someOtherFiles.go
| |-> go.mod
| |-> go.sum
|->utils
| |-> someOtherFiles.go
|->config
| |-> someOtherFiles.go
|-> go.mod
|-> go.sum
My root's go.mod is like this
module prex-kyc
go 1.13
require ({requiredDependencies})
And my validator's go.mod is like this (server's is analogue)
module validator
go 1.13
require (
prex-kyc v0.0.0-00010101000000-000000000000
{otherRequiredDependencies}
)
replace prex-kyc => ../
And in both validator's and server's main.go I do an import like this:
import (
"prex-kyc/utils"
{someOtherImports}
)
When I try to build either one of the projects i get this error: build validator: cannot load prex-kyc/config: malformed module path "prex-kyc/config": missing dot in first path element
I know there's nothing wrong with the code because it can be compiled in someone else's environment.
I have tried building using go versions 1.12 and 1.13 and both windows 10 and Debian Linux.
[SOLVED]
The issue was that i was importing utils like this:
import("prex-kyc/utils")
But actually there was no package utils inside module prex-kyc, (only directory utils) and every .go files in that directory had a different package name. By changing each one of them to "package utils" the issue was solved.
The error "missing dot in first path element" was really misleading though

Ginkgo package testing

I'm implementing test suite for brand new go app and decided to use ginkgo. The app has main function and several packages
.
|- main.go
|- types
| |-- user.go
| |-- post.go
|- server_pkg
| |-- users_controller.go
| |-- posts_controller.go
|- worker_pkg
| |-- users_worker.go
| |-- posts_worker.go
I ran ginkgo bootstrap in each package folder and added test files using ginkgo generate. Now I'm able to run tests for each package separately i.e.
cd server_pkg; ginkgo
The question is that: how to configure my application to run all tests for main function and packages using single command?
I can chain commands like ginkgo; cd server_pkg; ginkgo ..., but it does not look like good solution.
To run all test suits you should run this in command in your root catalog
ginkgo -r
Also is good practice as in normal test suits to run all test with race detector, also you could shuffle some test. You can run all this option by using
ginkgo -r --race --randomizeAllSpecs --randomizeSuites
#ttomalak thank you! It's exactly what I wanted
$ ginkgo -r

Can't create unittests IntelliJ

I am not able to select my testfolder in IntelliJ.
For some reason it wasn't there when I created this project, so I added it manually. After I did that, I went to project structure --> modules, where I marked it as a test.
I am simply not allowed to leave my source directory, when creating a testcase.
See image:
The problem doesn't occure when I create a new project, where the testfolder is set automaticly.
It seems that you have wrong project structure.
It should be as follows:
project-name
|-- pom.xml
|-- src
|-- main
| |-- java
| |-- resources
|-- test
|-- java
|-- resources
And don't mark the test directory as a test source root. You should mark the test/java (subdirectory under the test folder) instead:

Resources