Call function in a subfolder - go

Files structure:
main folder--main.go
|--Utils
inside Utils: Read-Words.go
So. I would like to use the functions inside Read-words.go in main.go.
How to achieve this?

On Read-Word.go set the package like parent folder on the top of file. So if that file on Utils folder. the package is Utils. Example :
package Utils
I suggest to give the folder name to not using a capital. Therefore your code like package utils . So, when you want to use the function on Read-Word.go, you could call the function like :
utils.<name_of_fuction>
Example :
utils.FindWord() // or etc

Related

Can't import a local sub directory

I have the main file "main.go" in the root of my project and there's also a subdirectory named "models" wherein there're multiple *.go files as well.
In the main.go there's this:
import "./models"
When I build the project
go build
this error appears:
main.go:16:2: package models is not in GOROOT (/usr/lib/go/src/models)
How to fix this?
Also, what if I created subdirectories inside "models" as well, how and from where would I have to import them? Later on I'll need this.
In you project, lets call it testmodule. You should have a subdirectory called models. In that subdirectory you use the packagename models.
When you import the subpackage in your main package you refer to it as testmodule/models. So you mainpackage/the subpackage you want to import

How to import the package in the Go

Did I do something wrong on importing a package under src?
My folder structure is like it:
- project
- src
helper.go( package utils)
main.go(package main)
And I want to use utils in the main.go
I write this:
import ("utils")
but give me the error,said can't import utils. I don't understand which is wrong. there is no other package under src.
Thank you for the help.
You must provide only 1 package within single folder. Consider to name package like the folder. In your case create folder "utils" and move your helper.go in there.
Please, don't forget to name public types, vars and funcs properly: start their names with uppercase symbol:
Finally your project would look like this:
Your helper.go would look like this:
package util
func SomeFunc() {
}
And your main.go would look like this:
package main
import "stackoverflowexamples/src/util"
func main(){
util.SomeFunc()
}

Using function from file in outer package

I'm a Golang beginner here and struggling with how to use function from the outer package
Here I have the source code as this tree below
-samplego
--pkg
--src
---github.com
----pkg1
-----a.go
-----pkg2
-------b.go
In b.go, I want to use the function from a.go but the compiler told me that the function is undefined even if I declare the function as public(with first character as capital letter).
I wonder If I have to import anything from a.go before using it but I think I would get circular import because in a.go also use function from b.go.
Thank you.
Maybe, a.go and pkg2 is flat, so, why don't you change your file view, like:
-samplego
--pkg
--src
---github.com
----pkg1
-----a.go
-----b.go
If you want keep your file structure, you should use mod, wait me some time.
-samplego
--pkg
--src
---github.com
----pkg1
-----a.go
-----pkg2
-------b.go
command below:
bash
cd $(SAMPLEGO_PATH) // change path to samplego exists
go mod init samplego
In b.go, import "samplego/src/github.com/pkg1", then import functions from a.go by pkg1.function;
In a.go, import "samplego/src/github.com/pkg1/pkg2", then import functions from b.go by pkg2.function;
Unfortunately, circular dependencies are not allowed until Go2. What you are trying to do can be made with the current Go version. You can refactor the code in order to create a package C that contains the data structures and the methods that have to be exported.
Remember that you need to use the go mod init YOUR_MODEL_NAME and then you can call the method (remember that have to be exported) from the other file using import YOUR_MODEL_NAME/package_name. And remember that every folder can contain only one package.

Trouble calling local package into main

I feel like this is probably an over-asked question on SO yet here it is again. I'm finding this simple task incredibly tedious in Go. Note that I have GO11MODULES set to ON, I'm not sure if this effects the whole package system (it shouldn't is what I'm assuming).
I have a package called "users" which contains a compiled Protocol Buffer (from a .proto file). I want to store it alongside a number of other definitions in a folder called protos. So that my structure looks like so:
- main.go
- protos
- users.go
- users.proto
- analytics.go
- analytics.proto
Pretty simple structure. Within the users.go file I'm defining package protos. Within main.go I'd like to import users "protos/users". When I do so I get this: build command-line-arguments: cannot load protos/users: cannot find module providing package protos/users.
I've followed (I think) other sample code that has done the same thing. Note that the folder structure is within $GOPATH/src/myapi.
Why is this more complicated than its proving to be?
If you are using package protos, then the package is protos. protos/users does not exist. Packages and package imports are directory-level, not file-level. The full import statement depends on the module declaration in your go.mod file, which defines the root of imports. E.g., if your go.mod begins with
module github.com/me/myapp
Then your import would be
import "github.com/me/myapp/protos"
This answer assumes that GO111MODULE is set to on. The question shows that you are setting GO11MODULES. I assume that this is a typo. Fix it if it's not a typoo.
Add file go.mod in same directory as main.go with the following contents:
module myapi
Change main to import "myapi/protos" instead of "protos/users"

How to make go build work with nested directories

In the process of learning go I was playing around with making my own libraries. Here is what I did: in my $GOPATH/src I have two folders: mylibs and test. The test folder has a file called test.go which contains
package test
import "mylibs/hi/saysHi"
func main() {
saysHi.SayHi()
}
The mylibs folder contains another folder called hi, which has a file called saysHi.go containing:
package saysHi
import "fmt"
func SayHi() {
fmt.Printf("Hi\n")
}
So the directory structure looks like this:
GOPATH/src
test
test.go
mylibs
hi
saysHi.go
The problem is that when I try to compile test it complains saying
cannot find package "mylibs/hi/saysHi" in any of:
[...]
$GOPATH/src/mylibs/hi/saysHi (from $GOPATH)
I have deliberately made the directory structure deeper than necessary. If I make a simpler directory structure where I place saysHi.go in $GOPATH/saysHi/saysHi.go then it works.
But I don't see a reason for why this wouldn't work. Any ideas?
Generally speaking, your directory name should match the package name. So if you define
package saysHi
and want to import it with
import "mylibs/hi/saysHi"
you should place it in a structure like this:
mylibs
hi
saysHi
saysHi.go
The name of the .go file(s) inside the package makes no difference to the import path, so you could call the .go file anything you like.
To explain it a bit further, the import path you use should be the name of the directory containing the package. But, if you define a different package name inside that directory, you should use that name to access the package inside the code. This can be confusing, so it's best to avoid it until you understand where it's best used (hint: package versioning).
It gets confusing, so for example, if you had your package in the path
mylibs
hi
saysHi.go
And inside saysHi.go defined,
package saysHi
Then in test.go you will import it with
import "mylibs/hi"
And use it with
saysHi.SayHi()
Notice how you import it with the final directory being hi, but use it with the name saysHi.
Final note
Just in case you didn't know the following: your test file is called test.go, and that's fine, if it's just as an example, and not an actual test file for saysHi.go. But if it is/were a file containing tests for saysHi.go, then the accepted Go standard is to name the file saysHi_test.go and place it inside the same package alongside saysHi.go.
One more final note
I mentioned how you are allowed to choose a different package name from the directory name. But there is actually a way to write the code so that it's less confusing:
import (
saysHi "mylibs/hi"
)
Would import it from the mylibs/hi directory, and make a note of the fact that it should be used with saysHi, so readers of your code understand that without having to go look at the mylibs/hi code.

Resources