Files
build-web-application-with-…/pt-br/02.1.md
2016-09-30 17:31:29 -03:00

3.3 KiB

2.1 Hello, Go

Antes de iniciar a construção de aplicações completas em Go, nós precisamos aprender a escrever um programa simples, afinal, você não pode construir um prédio sem antes construir sua fundação. Sendo assim, vamos aprender a sintaxe básica para executar alguns programas simples nessa seção.

Programa

Seguindo a reconhecida prática internacional, antes de aprender como programar em qualquer linguagem, você precisa saber como escrever um programa que imprime a famosa frase "Hello world".

Pronto pra esse primeiro desafio? Vamos lá!

package main

import "fmt"

func main() {
	fmt.Printf("Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界\n")
}

Isto irá imprimir a seguinte informação.

Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界

Explicação

Uma coisa que você precisa entender em primeiro lugar é que os programas Go são compostos de pacotes (identificados pela palavra-chave package).

package <pkgName> que no nosso exemplo é package main, identifica que esse código pertence ao pacote main e a palavra main indica que esse pacote será compilado em um programa ao invés de um pacote com extensão .a.

Todo programa executável possui um, e somente um, pacote main e este último precisa conter uma função nomeada como main sem nenhum argumento ou valores de retorno.

Para exibir a mensagem Hello, world…, foi utilizada uma função chamada Printf. Esta função faz parte do pacote fmt, então nós importamos esse pacote na terceira linha do código usando a instrução import "fmt".

The way to think about packages in Go is similar to Python, and there are some advantages: Modularity (break up your program into many modules) and reusability (every module can be reused in many programs). We just talked about concepts regarding packages, and we will make our own packages later.

On the fifth line, we use the keyword func to define the main function. The body of the function is inside of {}, just like C, C++ and Java.

As you can see, there are no arguments. We will learn how to write functions with arguments in just a second, and you can also have functions that have no return value or have several return values.

On the sixth line, we called the function Printf which is from the package fmt. This was called by the syntax <pkgName>.<funcName>, which is very like Python-style.

As we mentioned in chapter 1, the package's name and the name of the folder that contains that package can be different. Here the <pkgName> comes from the name in package <pkgName>, not the folder's name.

You may notice that the example above contains many non-ASCII characters. The purpose of showing this is to tell you that Go supports UTF-8 by default. You can use any UTF-8 character in your programs.

Conclusion

Go uses package (like modules in Python) to organize programs. The function main.main() (this function must be in the main package) is the entry point of any program. Go supports UTF-8 characters because one of the creators of Go is a creator of UTF-8, so Go has supported multiple languages from the time it was born.