Home >Backend Development >Golang >Discovering Go: Chapter First Steps in the Language

Discovering Go: Chapter First Steps in the Language

Susan Sarandon
Susan SarandonOriginal
2025-01-14 12:24:43563browse

Desbravando Go: Capítulo  Primeiros Passos na Linguagem

Starting my journey with the Go language, I decided to share my learnings. This is the first in a series of introductory Go tutorials.

How Go works

Go is a compiled language. When executing the compilation command, the source code (files with extension .go) is translated into machine language.

For direct execution, the run command is ideal for quick tests. For programs with continuous use or in production, the command build generates an executable binary.

Structure of a Go Program

All Go code is organized into packages, similar to libraries or modules in other languages ​​(although different from the package structure in Java, for example). A package consists of one or more files .go in the same directory.

Each file starts with the package declaration (e.g. package main).

Then the imports, using the import keyword, declares the required external packages. Just like in other languages, we use packages for specific tasks. The Go compiler is strict, preventing compilation if there are missing or unnecessary imports, ensuring only the essentials are included.

Functions are declared with func, followed by the name and parameters (if any): func nomeDaFuncao(parâmetros) { ... }.

Basic Syntax

Go generally dispenses with semicolons at the end of lines, except when two or more instructions are on the same line.

Line breaks indicate the end of an instruction. The opening braces { of a function must be on the same line as the declaration: func main() {.

Hello, World

<code class="language-go">package main

import "fmt"

func main() {
    fmt.Println("Hello, World")
}</code>

Next Steps

Did you like the content? Leave your comment or share your experience with Go! In the next chapters, we will explore more examples to consolidate learning.

The above is the detailed content of Discovering Go: Chapter First Steps in the Language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn