Methods of payment Abuse

How to create Hello World in Go language

25.11.2022, 23:31

We have already told you about the history of the Go programming language, its structure and syntax, now we can execute the simplest program on it, namely the classic Hello World. Ready? Then it won't take long to get the hang of it.

How to create a program

Let's create our first program. It is common in programming that all beginners start by displaying the line "Hello World!" on the screen. You can install Go on your system or use the online service play.golang.org. With its help, you can run your first program right in your browser. Let's work with the online service. Open the site.

Next, you will see the ready code of the first program:

package main
import ( "fmt")

func main() {
fmt.Println("Hello, playground")
}

How to create Hello World in Go language

In the first line we should specify that the file belongs to the main package, in the second line we import the fmt package, which is responsible for the standard input and output functions. Brackets are necessary to import several packages at a time. Names can be added separated by commas.

After that, the main function package will be updated. Next it will be executed with the program running. All Go programs include the main function. The main function is the so-called entry point. execution of the command starts from it, although in any dynamic language execution is top-down.

Inside the main function, let's call the fmt library. Previously, it was imported using the dot syntax. The printLn method prints the string to the screen. Go has adopted a certain syntax: the point is that even if the compiler doesn't throw an error because of an inconsistency, it is recommended to follow the syntax exactly.

It is common to use camel syntax for variable names, i.e. FooBar or Foobar instead of foo_bar. You may have already noticed that Go does not carry any characters before the opening bracket. Even if you put a line feed, you'll get an error:

How to create Hello World in Go language

Indentation is also standardized in Go, although there are no requirements here like in Python.