Getting Started With the Golang—More about Environment
In this blog, we follow up our previous discussion on Golang environment. After this class, you should begin creating simple Golang apps.
To review, your Golang apps all your Golang apps will be organized around the workspaces. Workspaces are the directories that contain three directories at its root (the “src”, the “pkg” and the “bin”).
The “src” contains the Golang source code files that you’ll organize around the packages. The “pkg” contains the package objects—which act as namespaces—that can be generated from either the standard libraries or you can create your packages. The “bin” has all the executable commands.
Let’s dive in and find out more about the structure of Golang programs.
Structure of Golang Programs
Just like C or C++ programs, the Golang are run in the package “main”. As a principle, the name of the package the same as the last element of your import path. The packages that are used from the Golang standard library are given short paths such as the “fmt” or the “net/http”.
However, this doesn’t mean that you’ll only be using the standard packages from the standard library. You can create your packages. If you create your packages, you should select a base path that doesn’t collide with future additions to your standard library.
After the main package statement, you should specify the “imports” you’ll be using in your code:
The “imports” are grouped into parenthesized statements. For instance, the code below illustrates how you can specify your “imports.”
import "fmt" import "math"
Alternatively, you can use the factored statements to define your “imports”. Here’s an example:
import ( "fmt" "math" )
Whenever you import a package, it’s important to refer to only its exported names. You’re now wondering, “What are exported names?” Well, an exported name is a name that’s accessible from outside the package. In Golang, a name is exported only if it starts with a capital letter.
For instance, “Bar” is an exported name because it’s starting with a capital letter. Similarly, “BAR” is also an exported name. However, “bar” isn’t an exported name because it doesn’t begin with a capital letter.
Golang Functions
Your Golang programs will be created around functions. Functions will act as procedure or routines that perform some operations to return a value. The Golang function that you define can take zero or more parameters. For instance, the function below takes two numbers of the type “integer” and calculates their product.
func multiply (a int, b int) int { return a*b }
When you declare your variables, the type should come after the variable name.
How to write the first application in Golang
In this example, we illustrate how you’ll create your first application using Golang. Here are steps that you should follow:
1.Create a new folder where you’d like to store your application. For instance, you can create a folder named “GolangApps” in ~/src/golang/ where ~ is your home directory.
2.If you’re using Linux, open the terminal and type the following commands at the command prompt:
-
- •mkdir src/golang
- •mkdir src/golang/GolangApps
3.Open your text editor (Notepad, Text Edit or Gedit) and type the following code:
package main import "fmt" //*This how comments are written in Golang func main () { fmt.Println("Hello World") }
4.Save your file using the “.go” file extension. For instance, you can save it as “hello.go”.
To run the program, do the following:
1.Open up a new terminal in your computer and type in the following commands at the command prompt:
-
- •cd src/golang/GolangApps to change to the directory where the file was saved. In this case, the ““hello.go” file.
- •go run hello.go to execute the file.
Now you can create any application that you want in Golang.
GET YOUR FREE Golang EBOOK!