In this article, we will tell you how to work with variables and structures. This is especially useful material for novice users. Recall that the language has static typing. In Python, Ruby or JavaScript you will have to perform several correctness checks. Go avoids this problem.
Let's consider the first example:
A new function, print
, is responsible for the output. Here you can see that you need to pass astring
to it. A variable of string
type will be created in the main
function. It will be passed to print
. If we pass it otherwise, we will get an error. You can change the type of the variable to int
, you will get an error:
cannot use "Hello World!" (type string) as type int in assignment
You will also get an error if you have declared a variable or imported a package but are not using it. There are many more things you can do with Go, one of them is structures. Let's complicate our program by creating a site
structure, with name
and url
fields, which will describe some site:
The structure will have a print
method that outputs "Welcome...". In main
, we initialized the structure and assigned values to its fields, and then called the print
method.
The Go programming language is characterized by a number of features and capabilities. We have seen for ourselves.