Golang import packages - go

I have some problem with Golang and include package. I have that scructure
src/
├── hello_world
│   ├── hello.go
│   └── math
│   └── add.go
hello.go file contains this code:
package main
import (
"fmt"
math "hello_world/math"
)
func main() {
fmt.Println("Hello World")
x := math.add(6, 5)
}
and add.go
package math
func add(x, y int) int {
return x + y
}
and when I do go run hello go I see:
evgen#laptop:~/go/src/hello_world$ go run hello.go
# command-line-arguments
./hello.go:10: cannot refer to unexported name math.add
./hello.go:10: undefined: "hello_world/math".add
GOPATH:
evgen#laptop:~/go/src/hello_world$ echo $GOPATH
/home/evgen/go
How fix it? Thanks you!

Outside of a package only the exported identifiers can be reached and referred to, that is identifiers that start with an uppercase letter.
So the easiest fix is to export your math.add() function by changing its name to Add() in math.go:
func Add(x, y int) int {
return x + y
}
And, of course, when you refer to it from the main.go:
x := math.Add(6, 5)
And as a side note, note that when importing your hello_world/math package you don't have to specify a new name to refer to its exported identifiers: by default it will be the last part of its import path, so this is equivalent to your imports:
import (
"fmt"
"hello_world/math"
)

Capitalize the function within your package that you want other functions to read:
func Add(x, y int) int {
return x + y
}
then call it in hello.go like this:
x := math.Add(6, 5)
Keeping them lower case does have its purpose, particularly if you want to protect it from inadvertent use outside the package.

And when calling Add in your main function, dont use this
x := math.Add(6 + 5)
Instead use this
x := math.Add(6, 5)

Functions, variables , anything coming from a different package have to start with a capital letter to make it visible for when importing into the main package.
example:
package main
import "fmt"
import "other/out"
func main(){
fmt.Println(out.X)
// hello
}
package other
var X string = "hi"

Related

Golang Antlr cannot find path for parser

I am trying to use a Golang parser generated by Antlr. but when I try to access it in my main file, it gives me the following error: build command-line-arguments: cannot find module for path path/to/parser my main file is this:
package main
import (
"fmt"
"./parser"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
func main() {
fmt.Println("Hello, World!")
is := antlr.NewInputStream("1 + 2 * 3")
// Create the Lexer
lexer := parser.NewHelloWorldLexer(is)
// Read all tokens
for {
t := lexer.NextToken()
if t.GetTokenType() == antlr.TokenEOF {
break
}
fmt.Printf("%s (%q)\n",
lexer.SymbolicNames[t.GetTokenType()], t.GetText())
}
}
Here is solution using go modules
Your project structure should look like this
.
├── go.mod
├── main.go
└── parser
└── parser.go
go.mod
module example.com/projectname
go 1.15
...
main.go
package main
import (
"fmt"
"example.com/projectname/parser"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
...
parser.go
package parser
import (
"github.com/antlr/antlr4/runtime/Go/antlr"
)
...

Issue with go package declaration containing more than 2 words separated by underscore

everyone, I'm confused by what I'm seeing; I have the following tree:
├── go.mod
├── main.go
└── server
├── server.go
└── server_integration_test.go
Let's say my module name (mod.go) is gotest. Content of server.go:
package server
type MyStruct struct {
Hello string
}
func (m MyStruct) SayHello() string {
return m.Hello
}
Contents of server_integration_test.go:
package server_integration_test
import (
"testing"
)
func TestIntegration(t *testing.T) {
t.Errorf("just gonna fail!")
}
And finally my main.go`:
package main
import (
"fmt"
"gotest/server"
)
func main() {
my := server.MyStruct{Hello: "my-struct"}
fmt.Println("from mystruct", my.SayHello())
}
When I run go build (or go test ./...), I receive the following error:
main.go:5:2: found packages server (server.go) and server_integration (server_integration_test.go) in /tmp/gotest/server
But if I change my server_integration_test.go to be:
package server_test
// ...
Everything works.
Can someone please explain what's happening here?
The supported package names for server package tests are server and server_test.
See test packages:
'Go test' recompiles each package along with any files with names matching the file pattern "*_test.go". These additional files can contain test functions, benchmark functions, and example functions. ...
Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary.
The _test suffix is applied to the name of the package under test (the documentation can be improved to make this fact more explicit).

Undefined Variables Within a Package During Build

I have two files within one package named db, one of which has a few unexported variables defined. Another one is a test file and would need to use these variables like so:
(This is the structure of the project)
$GOPATH/src/gitlab.com/myname/projectdir
├── main.go
└── db
├── add.go
└── add_test.go
(Here is a terse variation of the files)
db/add.go
package db
func Add(x, y int) int {
return x + y
}
// some other functions that use a and b from `add_test.go`
db/add_test.go
package db
import (
"testing"
)
var (
a = 1
b = 2
)
// test function use variables from add.go
func testAdd(t *testing.T) {
result := add(a, b)
if result != 3 {
t.Error(err)
}
}
Running go test within db/ directory passed, but once I ran go run main go it produced the following error:
db/add.go:: undefined: a
db/add.go:: undefined: b
Seems like add.go cannot find a and b from add_test.go during the build.
main.go
package main
import (
"fmt"
"gitlab.com/myname/projectdir/db"
)
func main() {
res := db.Add(1, 2)
fmt.Println(res)
}
Is this because add_test.go is not included during the build?
This is just the way how go tool works.
_test.go files are compiled only when you run go test. When a package is imported from another package any code from its _test.go files is not used.
Try running go build or go install from inside db package. It will fail.
Relative paths are touchy in Go. For one, I think you need to prefix them with import "./db". Another thing is that you should be in your $GOPATH/src location.
Try this:
move your files under the $GOPATH/src/project and $GOPATH/src/project/db directories.
prefix your import path with ./db for the DB package.
As for the IDE, that's all up to whatever plugins you are using. Try running the tools yourself: golint, go vet, oracle, etc to see the actual go warnings and errors.
Test functions should start with Test. that is what the documentation says.
func TestAdd(t *testing.T) {
result := Add(a, b)
if result != 3 {
t.Errorf("expected 3, got %d ", result)
}
}
Cheers.

Call a function from another package in Go

I have two files main.go which is under package main, and another file with some functions in the package called functions.
My question is: How can I call a function from package main?
File 1: main.go (located in MyProj/main.go)
package main
import "fmt"
import "functions" // I dont have problem creating the reference here
func main(){
c:= functions.getValue() // <---- this is I want to do
}
File 2: functions.go (located in MyProj/functions/functions.go)
package functions
func getValue() string{
return "Hello from this another package"
}
You import the package by its import path, and reference all its exported symbols (those starting with a capital letter) through the package name, like so:
import "MyProj/functions"
functions.GetValue()
You should prefix your import in main.go with: MyProj, because, the directory the code resides in is a package name by default in Go whether you're calling it main or not. It will be named as MyProj.
package main just denotes that this file has an executable command which contains func main(). Then, you can run this code as: go run main.go. See here for more info.
You should rename your func getValue() in functions package to func GetValue(), because, only that way the func will be visible to other packages. See here for more info.
File 1: main.go (located in MyProj/main.go)
package main
import (
"fmt"
"MyProj/functions"
)
func main(){
fmt.Println(functions.GetValue())
}
File 2: functions.go (located in MyProj/functions/functions.go)
package functions
// `getValue` should be `GetValue` to be exposed to other packages.
// It should start with a capital letter.
func GetValue() string{
return "Hello from this another package"
}
Export function getValue by making 1st character of function name capital, GetValue
you can write
import(
functions "./functions"
)
func main(){
c:= functions.getValue() <-
}
If you write in gopath write this import functions "MyProj/functions" or if you are working with Docker
In Go packages, all identifiers will be exported to other packages if the first letter of the identifier name starts with an uppercase letter.
=> change getValue() to GetValue()
you need to create a go.mod file in the root directory of your project: go mod init module_name
the name of exposed function should start with capital letter
import(
"module_name/functions"
)
func main(){
functions.SomeFunction()
}

How to call function from another file in Go

I want to call function from another file in Go. Can any one help?
test1.go
package main
func main() {
demo()
}
test2.go
package main
import "fmt"
func main() {
}
func demo() {
fmt.Println("HI")
}
How to call demo in test2 from test1?
You can't have more than one main in your package.
More generally, you can't have more than one function with a given name in a package.
Remove the main in test2.go and compile the application. The demo function will be visible from test1.go.
Go Lang by default builds/runs only the mentioned file. To Link all files you need to specify the name of all files while running.
Run either of below two commands:
$go run test1.go test2.go. //order of file doesn't matter
$go run *.go
You should do similar thing, if you want to build them.
I was looking for the same thing. To answer your question "How to call demo in test2 from test1?", here is the way I did it. Run this code with go run test1.go command. Change the current_folder to folder where test1.go is.
test1.go
package main
import (
L "./lib"
)
func main() {
L.Demo()
}
lib\test2.go
Put test2.go file in subfolder lib
package lib
import "fmt"
// This func must be Exported, Capitalized, and comment added.
func Demo() {
fmt.Println("HI")
}
A functional, objective, simple quick example:
main.go
package main
import "pathToProject/controllers"
func main() {
controllers.Test()
}
control.go
package controllers
func Test() {
// Do Something
}
Don't ever forget: Visible External Functions, Variables and Methods starts with Capital Letter.
i.e:
func test() {
// I am not Visible outside the file
}
func Test() {
// I am VISIBLE OUTSIDE the FILE
}
If you just run go run test1.go and that file has a reference to a function in another file within the same package, it will error because you didn't tell Go to run the whole package, you told it to only run that one file.
You can tell go to run as a whole package by grouping the files as a package in the run commaned in several ways. Here are some examples (if your terminal is in the directory of your package):
go run ./
OR
go run test1.go test2.go
OR
go run *.go
You can expect the same behavior using the build command, and after running the executable created will run as a grouped package, where the files know about eachothers functions, etc. Example:
go build ./
OR
go build test1.go test2.go
OR
go build *.go
And then afterward simply calling the executable from the command line will give you a similar output to using the run command when you ran all the files together as a whole package. Ex:
./test1
Or whatever your executable filename happens to be called when it was created.
Folder Structure
duplicate
|
|--duplicate_main.go
|
|--countLines.go
|
|--abc.txt
duplicate_main.go
package main
import (
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts)
f.Close()
}
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
countLines.go
package main
import (
"bufio"
"os"
)
func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
}
go run ch1_dup2.go countLines.go abc.txt
go run *.go abc.txt
go build ./
go build ch1_dup2.go countLines.go
go build *.go
You can import functions from another file by declaring the other file as a module. Keep both the files in the same project folder.
The first file test1.go should look like this:
package main
func main() {
demo()
}
From the second file remove the main function because only one main function can exist in a package. The second file, test2.go should look like below:
package main
import "fmt"
func demo() {
fmt.Println("HI")
}
Now from any terminal with the project directory set as the working directory run the command:
go mod init myproject.
This would create a file called go.mod in the project directory. The contents of this mod file might look like the below:
module myproject
go 1.16
Now from the terminal simply run the command go run .! The demo function would be executed from the first file as desired !!
as a stupid person who didn't find out what is going on with go module
should say :
create your main.go
in the same directory write this in your terminal
go mod init "your module name"
create a new directory and go inside it
create a new .go file and write the directory's name as package name
write any function you want ; just notice your function must starts with capital letter
back to main.go and
import "your module name / the name of your new directory"
finally what you need is writing the name of package and your function name after it
"the name of your new dirctory" + . + YourFunction()
and write this in terminal
go run .
you can write go run main.go instead.
sometimes you don't want to create a directory and want to create new .go file in the same directory, in this situation you need to be aware of, it doesn't matter to start your function with capital letter or not and you should run all .go files
go run *.go
because
go run main.go
doesn't work.
Let me try.
Firstly
at the root directory, you can run go mod init mymodule (note: mymodule is just an example name, changes it to what you use)
and maybe you need to run go mod tidy after that.
Folder structure will be like this
.
├── go.mod
├── calculator
│ └── calculator.go
└── main.go
for ./calculator/calculator.go
package calculator
func Sum(a, b int) int {
return a + b
}
Secondly
you can import calculator package and used function Sum (note that function will have Capitalize naming) in main.go like this
for ./main.go
package main
import (
"fmt"
"mymodule/calculator"
)
func main() {
result := calculator.Sum(1, 2)
fmt.Println(result)
}
After that
you can run this command at root directory.
go run main.go
Result will return 3 at console.
Bonus: for ./go.mod
module mymodule
go 1.19
ps. This is my first answer ever. I hope this help.

Resources