Gogland : Where to put source code files in package directory for rookie
Below is a simple guide for JetBrains Gogland IDE on how to add your own package and invoke the functions inside the package.
Wait?! Why write down this guide? Simply because one of my friends is having trouble doing so.
UPDATE:
As mentioned by dlsniper in the comment section below, there is a bit of inaccuracy regarding this post. Please see the follow up guide on the fix - https://socketloop.com/tutorials/gogland-single-file-versus-go-application-run-configurations
He is a Java developer learning Golang and Gogland at the same time. He got puzzled that he couldn't invoke a function although the source code files were placed in the same directory and the function name Echo()
starts with capital letter E
- which makes it exportable.
Maybe his Java background still influencing how he codes.
Anyway, let's create a new project under Gogland and create two new files. First is the main.go
and another is echo.go
- which contain a function that we will invoke from main.go
Now, typically all the newly created .go
files will be under the main project directory. You will need to place the files properly in order to make it work.
Below is an example screen shot of "wrong way"
to place your source code files that most rookie will probably encounter:
The proper way is to create a new directory and place the file containing the functions inside the new directory. For example, create a HelloPackage
directory and place the echo.go
file in.
and make sure that the echo.go
file first line is package HelloPackage
instead of package HelloGogland
package HelloPackage
func Echo() string {
return "Hello Gogland"
}
Click Run
button and you should see the following output:
Hello Gogland
Process finished with exit code 0
Hope this helps and happy coding!
See also : Gogland : Single File versus Go Application Run Configurations
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.3k Golang : Validate IPv6 example
+7k Golang : How to iterate a slice without using for loop?
+35.4k Golang : Get file last modified date and time
+8.7k Golang : Go as a script or running go with shebang/hashbang style
+5.7k Golang : Shuffle array of list
+4.5k Unix/Linux : How to pipe/save output of a command to file?
+6.2k Golang : Convert an executable file into []byte example
+27.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+7.8k Golang : Multiplexer with net/http and map
+23.8k Golang : How to validate URL the right way
+4.4k JavaScript : Rounding number to decimal formats to display currency
+33.3k Golang : How to check if slice or array is empty?