Let's create the first program in the Go language. To write the code, we'll need some kind of text editor. You can take any editor, such as the built-in Notepad or the popular Notepad++ or any other. A compiler is required to translate the source code into an application.
Let's define a folder on the hard drive to store the source code files. Let's say in my case it's the golang-examples folder. In this folder, create a new text file, which we will rename to main.go.
Open this file in any text editor and define the following code in it:
package main
import "fmt"
func main() {
fmt.Println("Welcome to Go Tutorials")
}
What does this program do? A Go program is defined in packages. The program code must be defined in a specific package. Therefore At the very beginning of the file, you can use the package statement to specify which package the file will belong to. In this case, it's the main package:
package main
When composing program code, we may need functionality from other packages. Go has a lot of built-in packages that contain code, performing certain actions. For example, in our program, we will display a message on the console. And for that, we need the Println function, which is defined in the FMT package. Therefore, with the second line, we use the import directive to include this package.
Next up is the main function. This is the main feature of any program on Go. Everything that is executed in the program is executed by the main function.
The definition of a function begins with the word func, followed by the name of the function, that is, main. After the name of the function in parentheses, the parameters are listed. Since the main function does not accept any parameters, empty parentheses are specified in this case.
Then, in curly brackets, the body of the main function is defined, i.e. the actions that the function performs.
func main
In our case, the function displays the line "Welcome to Go Tutorials" on the console. To do this, use the Println() function, which is defined in the fmt package. Therefore, when a function is called, the package name is specified first, followed by the function name separated by a period. In parentheses, the function passes the message that it should output to the console.
Now let's compile and execute this program. To do this, you need to pass the source file to the compiler and specify the desired command. To do this, open the command prompt (terminal) and navigate to the folder where the main.go source code file is stored (in my case, it's the go-examples folder).
Go is a compiler. Since the compiler path is automatically written to the PATH variable in the environment variables during installation, then we do not need to specify the full path but just write the name of the go application. Next is the run parameter, which says we just want to execute the program. And at the end, the actual main.go program file is indicated.
As a result, after execution, the console will display the message "Welcome to Go Tutorials".
This command executes, but does not compile, the program into a separate executable file. To compile it, run another command(go build).
After executing this command, another file will appear in the folder with the source file, which will be called main and which we can run. After that, again we can execute the program by running this file in the console: