snippetgoCritical
How to call function from another file in Go
Viewed 0 times
howfromfunctionfilecallanother
Problem
I want to call function from another file in Go. Can any one help?
How to call
test1.gopackage main
func main() {
demo()
}test2.gopackage main
import "fmt"
func main() {
}
func demo() {
fmt.Println("HI")
}How to call
demo in test2 from test1?Solution
You can't have more than one
More generally, you can't have more than one function with a given name in a package.
Remove the
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.Context
Stack Overflow Q#14155122, score: 150
Revisions (0)
No revisions yet.