Answer the question
In order to leave comments, you need to log in
How to include another file in golang?
Tell me, maybe a banal question, how to connect another file in golang?
I just started experimenting with it and so far I am writing in one file.
I would like to transfer part of the code to another and connect it to main.go
Answer the question
In order to leave comments, you need to log in
For beginners, there are two articles that are required reading. The first is " How to Write Go Code " and the second is " Understanding Golang Packages "
In short, Go doesn't include a file, but imports a module (package of files).
main.go:
package main
import "./mymodule"
func main() {
mymodule.MethodOne()
}
package mymodule
import "fmt"
func MethodOne() {
fmt.Println("Hello World!")
}
Please tell me, did you search for the answer to this question in Google?
https://golang.org/doc/code.html
Suppose we have a hello.go file with the following content
package main
import "fmt"
const(
constString="Hello, constant!"
)
func main() {
fmt.Println(constString)
}
package main // Это важно!
const(
constString="Hello, constant!"
)
package main
import "fmt"
func main() {
fmt.Println(constString)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question