Go Program Structure
# Go Program Structure
Before we start learning the basic building blocks of the Go programming language, let's first understand the structure of the simplest Go program.
* * *
## Go Hello World Example
The basic components of a Go program are as follows:
* Package declaration
* Import packages
* Functions
* Variables
* Statements & Expressions
* Comments
Next, let's look at a simple code that outputs "Hello World!":
## Example
package main
import"fmt"
func main(){
/* This is my first simple program */
fmt.Println("Hello, World!")
}
Let's look at the various parts of the above program:
1. The first line of code _package main_ defines the package name. You must specify which package this file belongs to in the first non-comment line of the source file, e.g., package main. package main represents an executable program, and every Go application contains a package named main.
2. The next line _import "fmt"_ tells the Go compiler that this program needs to use the fmt package (its functions, or other elements). The fmt package implements functions for formatted I/O (input/output).
3. The next line _func main()_ is the function where program execution begins. The main function is required in every executable program and is generally the first function executed after startup (if there is an init() function, that function will be executed first).
4. The next line /*...*/ is a comment, which will be ignored during program execution. Single-line comments are the most common form of comment; you can use single-line comments starting with // anywhere. Multi-line comments, also known as block comments, start with /* and end with */ and cannot be nested. Multi-line comments are generally used for package documentation or commenting out blocks of code.
5. The next line _fmt.Println(...)_ can output a string to the console and automatically adds a newline character n at the end.
Using fmt.Print("hello, worldn") will produce the same result.
Both the Print and Println functions also support using variables, e.g., fmt.Println(arr). Unless otherwise specified, they will output the variable arr to the console using the default print format.
6. When an identifier (including constants, variables, types, function names, struct fields, etc.) starts with an uppercase letter, e.g., Group1, then the object using this form of identifier can be used by code in external packages (the client program needs to import this package first). This is called exported (like public in object-oriented languages); identifiers starting with a lowercase letter are not visible outside the package, but are visible and usable throughout the package (like protected in object-oriented languages).
* * *
## Executing a Go Program
Let's see how to write and execute Go code. The steps are as follows:
1. Open an editor like Sublime2 and add the above code to the editor.
2. Save the above code as _hello.go_
3. Open the command line and navigate to the directory where the program file is saved.
4. Enter the command _go run hello.go_ and press Enter to execute the code.
5. If everything is correct, you will see the output _"Hello World!"_ on the screen.
$ go run hello.go Hello, World!
6. We can also use the go build command to generate a binary file:
$ go build hello.go $ ls hello hello.go $ ./hello Hello, World!
### Note
Note that { cannot be placed on a separate line, so the following code will produce an error when run:
## Example
package main
import"fmt"
func main()
{// Error, { cannot be on a separate line
fmt.Println("Hello, World!")
}
YouTip