E
E
Evgeniy Zavyalov2014-05-04 18:19:23
go
Evgeniy Zavyalov, 2014-05-04 18:19:23

golang. Call a function from another file

Go has just started to try. Question: There are two files. main.go and test.go. files are in the same directory. In LiteIDE under Win it is highlighted that "myPrint func()" can be inserted. And when compiling on Debian, the message is
# command-line-arguments
./main.go:20: undefined: myPrint
----------------
File main.go
------ ----------
package main
import (
"fmt"
)
func main(){
myPrint()
fmt.Println("111")
}
--------------- -
File test.go
----------------
package main
import (
"fmt"
)
func myPrint()
fmt.Println("222")
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
Tyranron, 2014-05-04
@profit

If the files are in the same directory and both are in package main, then there should be no problem, even if you name with a small letter.
I suspect that the problem is in the way the program is launched. For go run, this will not work, it can only chain single files. You need to compile the directory and everything should be fine.

T
theaidem, 2014-05-04
@theaidem

Make the function name capitalized (MyPrint()) and it will become visible in other files of the main package

I
Ildar72, 2018-04-20
@Ildar72

go run main.go test.go

E
Eldiyar Barynbekov, 2020-11-21
@eldiyar_321

If you want to run through the console, then separate them into different packages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question