Skip to content

Commit 1b3d359

Browse files
committed
Add packages concept text, use in party-robot exercise.
Fixes #1586
1 parent 5efd8f2 commit 1b3d359

File tree

7 files changed

+52
-69
lines changed

7 files changed

+52
-69
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"blurb": "TODO: add blurb",
33
"authors": ["ErikSchierboom"],
4-
"contributors": []
4+
"contributors": ["bobtfish"]
55
}

concepts/packages/about.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@ In Go an application is organized in packages. A package is a collection of sour
66
package greeting
77
```
88

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:
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.
11+
12+
```go
13+
package greeting
14+
15+
import "fmt"
16+
```
17+
18+
Multiple packages can be imported at once by encoding them in brackets.
19+
20+
```go
21+
package greeting
22+
23+
import (
24+
"errors"
25+
"fmt"
26+
)
27+
```
28+
29+
Third party packages are imported using the url of the package's location.
30+
A package can be given an alias when importing.
31+
This can be used to avoid package name conflicts:
1032

1133
```go
1234
package greeting

concepts/packages/introduction.md

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,27 @@
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. 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.
124

135
```go
146
package greeting
157
```
168

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.
2511

2612
```go
27-
count := 1 // Assign initial value
28-
count = 2 // Update to new value
13+
package greeting
2914

30-
// Compiler error when assigning different type
31-
// count = false
15+
import "fmt"
3216
```
3317

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.
3519

3620
```go
3721
package greeting
3822

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

concepts/packages/links.json

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
[
22
{
3-
"url": "https://golang.org/ref/spec#Assignments",
4-
"description": "assignment"
3+
"url": "https://golang.org/ref/spec#Import_declarations",
4+
"description": "import-declaration"
55
},
66
{
7-
"url": "https://golang.org/ref/spec#Assignments",
8-
"description": "assignment"
9-
},
10-
{
11-
"url": "https://golang.org/ref/spec#Operators",
12-
"description": "operators"
13-
},
14-
{
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" }
7+
"url": "https://golang.org/ref/spec#Package_clause",
8+
"description": "package"
9+
}
3510
]

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"
@@ -1816,6 +1817,11 @@
18161817
"slug": "numbers",
18171818
"uuid": "82cff17e-88cd-4707-a6d7-b5143251f029"
18181819
},
1820+
{
1821+
"name": "Packages",
1822+
"slug": "packages",
1823+
"uuid": "2062bff8-33ed-4081-8ad6-774b3410c450"
1824+
},
18191825
{
18201826
"name": "Range Iteration",
18211827
"slug": "range-iteration",

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

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

1414
```go
15+
import "fmt"
16+
1517
food := "taco"
1618
fmt.Sprintf("Bring me a %s", food)
1719
// Returns: Bring me a taco
@@ -21,6 +23,8 @@ In Go floating point values are conveniently formatted with Sprintf's verbs: `%g
2123
All three verbs allow the field's width and numeric position to be controlled.
2224

2325
```go
26+
import "fmt"
27+
2428
f := 4.3242
2529
fmt.Sprintf("%.2f", f)
2630
// 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)