Easy way to import files in Go
Learn Go the Game of Thrones way!
If you just started learning Go, you will soon get stuck with the files importing.
The simple program
Let’s say, we have an object called Lannister, it has a Roar() method to say “Hear me roar!!!”
With the single file solution, we put everything in our main.go, so the program should be like this:
But for structuring purpose, you need to separate the “class” Lannister to Lannister.go file, there are 2 cases of the external file importing now:
Files are in the same package
It’s easy if all the files you need to import are in the same main package as the main function.
You don’t have to include it, it’s automatically imported during the build.
Our program now looks like this:
Lannister.go
main.go
Files are in different packages
If you decided to put your files in the different package instead of main. It will be a bit complex to build and import the file.
First, you need to put your files in the package named folder.
For example, I will put my Lannister.go in app package, so my project should look like this:
And here is the code: