|
1 | 1 | # Introduction |
2 | 2 |
|
3 | | -TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction |
4 | | - |
5 | | -## packages |
6 | | - |
7 | | -## functions |
8 | | - |
9 | | -## variables |
10 | | - |
11 | | -In Go an application is organized in packages. A package is a collection of source files located in the same folder. All source files in a folder must have the same package name at the top of the file. The package name is preferred to be the same as the folder it is located in. |
| 3 | +In Go an application is organized in packages. A package is a collection of source files located in the same folder. All source files in a folder must have the same package name at the top of the file. The package is preferred to be the same as the folder it is located in. |
12 | 4 |
|
13 | 5 | ```go |
14 | 6 | package greeting |
15 | 7 | ``` |
16 | 8 |
|
17 | | -Go is a statically-typed language, which means that everything has a type at compile-time. Assigning a value to a name is referred to as defining a variable. A variable can be defined either by explicitly specifying its type, or by assigning a value to have the Go compiler infer its type based on the assigned value. |
18 | | - |
19 | | -```go |
20 | | -var explicit int // Explicitly typed |
21 | | -implicit := 10 // Implicitly typed |
22 | | -``` |
23 | | - |
24 | | -The value of a variable can be assigned using the `:=` and updated using the `=`. Once defined, a variable's type can never change. |
| 9 | +Go provides an extensive standard library of packages which you can use in your program using the `import` keyword. |
| 10 | +Standard library packages are imported using their name. |
25 | 11 |
|
26 | 12 | ```go |
27 | | -count := 1 // Assign initial value |
28 | | -count = 2 // Update to new value |
| 13 | +package greeting |
29 | 14 |
|
30 | | -// Compiler error when assigning different type |
31 | | -// count = false |
| 15 | +import "fmt" |
32 | 16 | ``` |
33 | 17 |
|
34 | | -A function can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. A function can also have multiple return values which must also be explicitly typed. Values are returned from functions using the `return` keyword. To allow a function to be called by code in other packages, the name of the function must start with a capital letter. |
| 18 | +Multiple packages can be imported at once by encoding them in brackets. |
35 | 19 |
|
36 | 20 | ```go |
37 | 21 | package greeting |
38 | 22 |
|
39 | | -// Hello is a public function |
40 | | -func Hello(name string) string { |
41 | | - return hello(name) |
42 | | -} |
43 | | - |
44 | | -// hello is a private function |
45 | | -func hello(name string) string { |
46 | | - return "Hello " + name |
47 | | -} |
| 23 | +import ( |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | +) |
48 | 27 | ``` |
49 | | - |
50 | | -Invoking a function is done by specifying the function name and passing arguments for each of the function's parameters. |
51 | | - |
52 | | -Go supports two types of comments. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. |
0 commit comments