Skip to content

Commit aa427a4

Browse files
committed
Add packages concept text, use in party-robot exercise.
Fixes #1586
1 parent 20d3dc4 commit aa427a4

File tree

9 files changed

+58
-83
lines changed

9 files changed

+58
-83
lines changed

concepts/functions/introduction.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ TODO: the content below is copied from the exercise introduction and probably ne
88

99
## variables
1010

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.
12-
13-
```go
14-
package greeting
15-
```
16-
1711
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.
1812

1913
```go
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"blurb": "TODO: add blurb",
3-
"authors": ["ErikSchierboom"],
2+
"blurb": "Packages are the unit of code reuse in Go",
3+
"authors": ["bobtfish"],
44
"contributors": []
55
}

concepts/packages/about.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
# About
22

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.
3+
In Go an application is organized in packages.
4+
A package is a collection of source files located in the same folder.
5+
All source files in a folder must have the same package name at the top of the file.
6+
The package is preferred to be the same as the folder it is located in.
47

58
```go
69
package greeting
710
```
811

9-
Standard library packages are imported using their name. Other packages are imported using the url of the package's location. A package can be given an alias when importing. This can be used to avoid package name conflicts:
12+
Go provides an extensive standard library of packages which you can use in your program using the `import` keyword.
13+
Standard library packages are imported using their name.
14+
15+
```go
16+
package greeting
17+
18+
import "fmt"
19+
```
20+
21+
Multiple packages can be imported at once by encoding them in brackets.
22+
23+
```go
24+
package greeting
25+
26+
import (
27+
"errors"
28+
"fmt"
29+
)
30+
```
31+
32+
Third party packages are imported using the url of the package's location.
33+
A package can be given an alias when importing.
34+
This can be used to avoid package name conflicts:
1035

1136
```go
1237
package greeting

concepts/packages/introduction.md

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,20 @@
11
# Introduction
22

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.
4+
A package is a collection of source files located in the same folder.
5+
All source files in a folder must have the same package name at the top of the file.
6+
The package is preferred to be the same as the folder it is located in.
127

138
```go
149
package greeting
1510
```
1611

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.
25-
26-
```go
27-
count := 1 // Assign initial value
28-
count = 2 // Update to new value
29-
30-
// Compiler error when assigning different type
31-
// count = false
32-
```
33-
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.
12+
Go provides an extensive standard library of packages which you can use in your program using the `import` keyword.
13+
Standard library packages are imported using their name.
3514

3615
```go
3716
package greeting
3817

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-
}
18+
import "fmt"
4819
```
4920

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 `*/`.

concepts/packages/links.json

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
11
[
22
{
3-
"url": "https://golang.org/ref/spec#Assignments",
4-
"description": "assignment"
3+
"url": "https://tour.golang.org/basics/1",
4+
"description": "Tour of Go: Packages"
55
},
66
{
7-
"url": "https://golang.org/ref/spec#Assignments",
8-
"description": "assignment"
7+
"url": "https://tour.golang.org/basics/2",
8+
"description": "Tour of Go: Imports"
99
},
1010
{
11-
"url": "https://golang.org/ref/spec#Operators",
12-
"description": "operators"
11+
"url": "https://golang.org/ref/spec#Import_declarations",
12+
"description": "import-declaration-spec"
1313
},
1414
{
15-
"url": "https://golang.org/ref/spec#Exported_identifiers",
16-
"description": "public-vs-private"
17-
},
18-
{
19-
"url": "https://golang.org/ref/spec#Method_declarations",
20-
"description": "methods"
21-
},
22-
{
23-
"url": "https://golang.org/ref/spec#Exported_identifiers",
24-
"description": "public-vs-private"
25-
},
26-
{
27-
"url": "https://golang.org/ref/spec#Return_statements",
28-
"description": "return"
29-
},
30-
{
31-
"url": "https://golang.org/ref/spec#Exported_identifiers",
32-
"description": "public-vs-private"
33-
},
34-
{ "url": "https://golang.org/ref/spec#Comments", "description": "comments" }
15+
"url": "https://golang.org/ref/spec#Package_clause",
16+
"description": "package-spec"
17+
}
3518
]

concepts/variables/introduction.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# Introduction
22

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 name is preferred to be the same as the folder it is located in.
4-
5-
```go
6-
package greeting
7-
```
8-
93
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.
104

115
```go

config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
"name": "Party Robot",
6262
"uuid": "b3594c44-1a68-489d-a446-16369247c84c",
6363
"concepts": [
64-
"string-formatting"
64+
"string-formatting",
65+
"packages"
6566
],
6667
"prerequisites": [
6768
"basics"
@@ -1821,6 +1822,11 @@
18211822
"slug": "numbers",
18221823
"uuid": "82cff17e-88cd-4707-a6d7-b5143251f029"
18231824
},
1825+
{
1826+
"name": "Packages",
1827+
"slug": "packages",
1828+
"uuid": "2062bff8-33ed-4081-8ad6-774b3410c450"
1829+
},
18241830
{
18251831
"name": "Range Iteration",
18261832
"slug": "range-iteration",

exercises/concept/party-robot/.docs/introduction.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Go provides an in-built package called `fmt` (format package) which offers a var
44
The most commonly used function is `Sprinf`, which uses verbs like `%s` to interpolate values into a string and returns that string.
55

66
```go
7+
import "fmt"
8+
79
food := "taco"
810
fmt.Sprintf("Bring me a %s", food)
911
// Returns: Bring me a taco
@@ -13,6 +15,8 @@ In Go floating point values are conveniently formatted with Sprintf's verbs: `%g
1315
All three verbs allow the field's width and numeric position to be controlled.
1416

1517
```go
18+
import "fmt"
19+
1620
f := 4.3242
1721
fmt.Sprintf("%.2f", f)
1822
// Returns: 4.32

exercises/concept/party-robot/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"tehsphinx"
55
],
66
"contributors": [
7-
"oanaOM"
7+
"oanaOM",
8+
"bobtfish"
89
],
910
"files": {
1011
"solution": [

0 commit comments

Comments
 (0)