User:Soniakeys/Go/Hello World

Program description
Hello World is familiar to programmers of almost any language. The program simply prints a short message to the screen. It is thus seen as a minimal program that provides some indication that it has run successfully.

Minimal as it is, this program introduces a number of elements common to almost all Go programs. The program is shown immediately below, and following that, it is explained in detail. This format will be followed for each example program in this book. The explanations will cover the workings of the program and in the process, introduce elements of the Go programming language.

Complete source code
package main

import "fmt"

func main { fmt.Println("Hello, 世界") } That's it! In fact, it's rather large, as Hello World programs go. In many programming languages, Hello World can be written in a single line. Often though, these are scripting languages meant for small tasks. Go, on the other hand, is a compiled language designed to support complex programs and large development projects. Go's principle of requiring important things to be explicitly stated gives Hello World a few extra lines of code.

Source file organization
Go source code has a simple organization with only a few requirements. A package clause must come first, followed by any number of import declarations, followed by any number of other declarations. In Hello World, there is just one import declaration, and just one other declaration, a function declaration.

A number of source files can be compiled together to form a package. A number of packages can then be linked together to form an executable program.

Package clause
package main This line is required, and for stand-alone programs like Hello World, it will be exactly package main. A complete Go program must have a package main, perhaps in addition to other packages. Source code to other packages will start with a different package identifier, "package fmt," for example. The package clause feels like boilerplate on a small program like this, but is necessary to identify the package as the one in which execution begins.

Import declaration
import "fmt" An import declaration lists one or packages that will be used within the source file. Here we are declaring that we will use something from the package fmt. fmt is a package in Go's standard library and it contains functions for formatting text for output, and for outputting text.

Main function
main and fmt.Println are Go functions. Another word for this kind of function is procedure. The word function sometimes has a more specific meaning in contexts of computer science or mathematics, but here we are using it in the sense of a procedure—a syntactic structure in a programming language that...does something. Go is called a procedural language because it is structured by these procedures.

This page also mentions values and types. string is an example of a Go type. "Hello, 世界" is an example of a value of type string. For short, we simply call it a string. fmt.Println is a function that can take a string and do something with it (print it out to screen.)

func main { This is a top level function declaration, and is special because it is called main. When a Go program starts, it always starts with the function main. Function main is thus required to exist, and is required to exist in package main.

Function declarations start with the keyword func. This particular declaration is about as simple as one can be; it consists of only the name of the function (main) and an empty parameter list. The opening brace marks the start of the function body. It is required to be on the same line as the function declaration.

Println function
fmt.Println("Hello, 世界") Our function body consists of this one line. It is a function call, to the function Println in the standard library package fmt. In this case it prints the string "Hello, 世界" to the screen, followed by a newline. This is "Hello, World" of course, with "World" in Chinese characters.

"Hello, 世界" is an example of a string literal. A literal is a value that is provided directly, at that place in the source code, as opposed to being computed or constructed elsewhere. string is a built-in data type in Go. A string is represented as an array of bytes with the added condition that it is immutable—it cannot be changed once it is created.

UTF-8 encoding
In this case we are storing UTF&#8209;8 encoded text in the string. It is not a strict requirement that strings contain UTF&#8209;8, but Go is designed to work naturally with UTF&#8209;8 and situations where Go strings are used to hold data other than UTF&#8209;8 encoded text will be uncommon.

An advantage of UTF&#8209;8 is that 7&#8209;bit ASCII is also valid UTF&#8209;8. This allows you to work with ASCII text easily, without any special consideration. If you can configure your operating system and text editor to work with UTF&#8209;8 however, you can work with the entire Unicode character set almost as easily.

Go source files are encoded as UTF&#8209;8 Unicode. This allows our string literal to simply be the UTF&#8209;8 text between the double quotes in the source file. Also a number of features of the Go language and a number of functions in the Go standard library interpret strings as UTF&#8209;8.