We have already told you how the GO language came to be. Now it's time to learn the syntax of the language. This is an important part of learning it. If you are not already familiar with the history, we recommend that you familiarize yourself with this material.
The greatest importance of any program is the structure. It has a lot in common with Java and Python. The program is divided into separate packages Package, which are essentially a replacement for include or modules in Python. In addition to package, individual scope customization can be done.
You can import a package into a program using import:
import name_pocket
Every program includes a set of variables. Go is a strictly typed language; each variable must be sent with its type before it can be used:
var variable type name
Russian is allowed in variables. If you set a variable to a value right away, the language will match its type:
var variable name := value
The user can create pointers.
Add an asterisk before the variable name:
var *variable type name
To access a variable declared inside a package from another package, note that only capitalized variables are accessible from the outside:
package_name.variable
name of the package.function_name
Control instructions are very similar in their syntax to the ones we are used to in C:
if the condition {
actions
}
The for loop here is exactly the same as in C, but without parentheses, so it looks even simpler:
for i := 0; i <= limiter; i++ {
actions
}
The golang functions are declared with the func directive, and in it you can specify not only the parameters, but also the return variables:
func function_name (accepted variables) (returned variables) {
actions
}
It's important to note that no semicolon is placed after strings. Instead of OOP classes, Go uses structures, which can have fields and methods, and can implement interfaces. To declare a structure, the type instruction is used:
type struct_name {
field_name field type
}
In addition to fields, structures can have methods, which allows them to be used as classes. Declaring a method is slightly different from a golang function:
func (designator_name *structure type) method_name() {
actions
}
Structures objects are created in the same way as regular variables, and their fields can be accessed using a dot:
object_name.method_name(parameters)
We refer to fields with a dot, too:
object_name.field_name
Now you know the basics of the language and it's time to move closer to practice. Next will be Go programming and Golang examples, minimum of theory.