A Go program is stored in one or more files. Each code file must belong to a package. And at the beginning of each file, The package to which the file belongs must be declared. A package is declared using the package keyword.
Functionality from other packages can be used in the file. In this case, you must import the packages you are using the import keyword. Imported packages should come after declaring the package for the current file:
package main
import "fmt"
For example, in this case, the current file will be in the main package. And then it connects the fmt package.
Moreover, the main package of the program should be called "main". This is because it is this package that determines that an executable file of the application will be created, which, after compilation, can be run for execution.
After the other packages are attached, declarations of types, variables, functions, and constants are placed.
In this case, the entry point to the applications is a function named main. It must be defined in the program. Everything that is done in the program is executed in the main function.
package main
import "fmt"
func main() {
fmt.Println("Go Tutorials wiht TechieClues")
}
The basic element of the program is the instructions. For example, a function call represents a separate statement. Each statement performs a specific action and is placed on a new line:
package main
import "fmt"
func main() {
fmt.Println("Go lang")
fmt.Println("Docker")
fmt.Println("Kubernetes")
}
Here, the main function contains three instructions that output a string to the console, and each of the instructions is placed on a new line.
You can place several instructions on the same line, but then they must be separated by a semicolon:
package main
import "fmt"
func main() {
fmt.Println("Go lang");
fmt.Println("Docker");
fmt.Println("Kubernetes")
}
At the same time, placing instructions on a new line represents a more readable format, so it is preferable to use.
A program can have comments. Comments are used to describe the actions performed by the program or some of its parts. When compiling comments are not taken into account and do not have any impact on the operation of the application. Comments can be single-line or multi-line.
A one-line comment is placed in one line after a double slash. Everything that comes after these symbols is perceived by the compiler as a comment.
A multiline comment is enclosed between the /* and */ characters and can span multiple lines:
/*
This a multi-line comments
*/
// this is a single line comment