Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions concepts/type-definitions/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "Go allows you to define custom types.",
"authors": ["bobtfish"],
"contributors": []
}
46 changes: 46 additions & 0 deletions concepts/type-definitions/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# About

## Struct types

We've already seen struct types; to recap a `struct` is a sequence of named elements called _fields_, each field having a name and type.
The name of a field must be unique within the struct.
`Structs` can be compared with the _class_ in the Object Oriented Programming paradigm.

You create a new struct by using the `type` and `struct` keywords, then explicitly define the name and type of the fields as shown in the example below.

```go
type StructName struct{
field1 fieldType1
field2 fieldType2
}
```

## Non-struct types

It's also possible to define non-struct types which you can use as an alias for a built in type declarations.

```go
type Name string
func SayHello(n Name) {
fmt.Printf("Hello %s\n", n)
}
n := Name("Fred")
SayHello(n)
// Output: Hello Fred
```

You can also define non-struct types composed of arrays and maps.

```go
type Names []string
func SayHello(n Names) {
for _, name := range n {
fmt.Printf("Hello %s\n", name)
}
}
n := Names([]string{"Fred", "Bill"})
SayHello(n)
// Output:
// Hello Fred
// Hello Bill
```
46 changes: 46 additions & 0 deletions concepts/type-definitions/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Introduction

## Struct types

We've already seen struct types; to recap a `struct` is a sequence of named elements called _fields_, each field having a name and type.
The name of a field must be unique within the struct.
`Structs` can be compared with the _class_ in the Object Oriented Programming paradigm.

You create a new struct by using the `type` and `struct` keywords, then explicitly define the name and type of the fields as shown in the example below.

```go
type StructName struct{
field1 fieldType1
field2 fieldType2
}
```

## Non-struct types

It's also possible to define non-struct types which you can use as an alias for a built in type declarations.

```go
type Name string
func SayHello(n Name) {
fmt.Printf("Hello %s\n", n)
}
n := Name("Fred")
SayHello(n)
// Output: Hello Fred
```

You can also define non-struct types composed of arrays and maps.

```go
type Names []string
func SayHello(n Names) {
for _, name := range n {
fmt.Printf("Hello %s\n", name)
}
}
n := Names([]string{"Fred", "Bill"})
SayHello(n)
// Output:
// Hello Fred
// Hello Bill
```
18 changes: 18 additions & 0 deletions concepts/type-definitions/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"url": "https://tour.golang.org/moretypes/2",
"description": "A Tour of Go"
},
{
"url": "https://go101.org/article/type-system-overview.html",
"description": "Go Type System Overview"
},
{
"url": "https://medium.com/rungo/structures-in-go-76377cc106a2",
"description": "Structures in Go"
},
{
"url": "https://gobyexample.com/structs",
"description": "Go by Example: Structs"
}
]
13 changes: 10 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@
],
"prerequisites": [
"structs",
"string-formatting"
"string-formatting",
"type-definitions"
],
"status": "beta"
},
Expand Down Expand Up @@ -219,13 +220,14 @@
"slug": "chessboard",
"uuid": "b5f1c789-adf6-487c-95b0-c6bce23d711b",
"concepts": [
"range-iteration"
"range-iteration",
"type-definitions"
],
"prerequisites": [
"conditionals-if",
"maps",
"slices",
"methods"
"structs"
],
"status": "beta"
},
Expand Down Expand Up @@ -1851,6 +1853,11 @@
"slug": "type-conversion",
"uuid": "4f3d535e-cec8-4303-ac5a-ed91a53bd594"
},
{
"name": "Type Definitions",
"slug": "type-definitions",
"uuid": "ef4bfcfb-fa81-4d80-be63-a50c47653490"
},
{
"name": "Zero Values",
"slug": "zero-values",
Expand Down
6 changes: 3 additions & 3 deletions exercises/concept/chessboard/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

As a chess enthusiast, you would like to write your own version of the game. Yes, there maybe plenty of implementations of chess available online already, but yours will be unique!

Each square of the chessboard is identified by a letter-number pair. The vertical columns of squares, called files, are labeled A through H. The horizontal rows of squares, called ranks, are numbered 1 to 8.
Each square of the chessboard is identified by a letter-number pair. The vertical columns of squares, called files, are numbered 1 through 8. The horizontal rows of squares, called ranks, are numbered 1 to 8.

## 1. Given a Chessboard and a Rank, count how many squares are occupied

Decorate the `Chessboard` type with the `CountInRank(rank byte) int` function.
Decorate the `Chessboard` type with the `CountInRank(rank int) int` function.
It should count occupied squares ranging over a map. Return an integer.

```go
CountInRank('A')
CountInRank(1)
// => 6
```

Expand Down
22 changes: 4 additions & 18 deletions exercises/concept/chessboard/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,31 @@ for i := range xi {
// 2
```

Last but not least, if you are required to perform some action but you are not
interested in values nor keys of the slice or map, you can omit both index and
value:

```go
xi := []int{10, 20, 30}
count := 0
for range xi {
count++
}
// count value:
// 3
```

## Non-struct types

You've previously seen defining struct types, but it's also possible to define non-struct types which you can use as an alias for a built in type declaration, and you can define reciever functions on them to extend them in the same way as struct types.

```go
type Name string
func (n Name) SayHello() {
func SayHello(n Name) {
fmt.Printf("Hello %s\n", n)
}
n := Name("Fred")
n.SayHello()
SayHello(n)
// Output: Hello Fred
```

You can also define non-struct types composed of arrays and maps.

```go
type Names []string
func (n Names) SayHello() {
func SayHello(n Names) {
for _, name := range n {
fmt.Printf("Hello %s\n", name)
}
}
n := Names([]string{"Fred", "Bill"})
n.SayHello()
SayHello(n)
// Output:
// Hello Fred
// Hello Bill
Expand Down
14 changes: 7 additions & 7 deletions exercises/concept/chessboard/.meta/exemplar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package chessboard
// Rank stores if a square is occupied by a piece
type Rank []bool

// Chessboard contains eight Ranks, accessed with values from 'A' to 'H'
type Chessboard map[byte]Rank
// Chessboard contains eight Ranks, accessed with values from '0' to '7'
type Chessboard map[int]Rank

// CountInRank returns how many squares are occupied in the chessboard,
// within the given rank
func (cb Chessboard) CountInRank(rank byte) (ret int) {
func CountInRank(cb Chessboard, rank int) (ret int) {
for _, r := range cb[rank] {
if r {
ret++
Expand All @@ -19,7 +19,7 @@ func (cb Chessboard) CountInRank(rank byte) (ret int) {

// CountInFile returns how many squares are occupied in the chessboard,
// within the given file
func (cb Chessboard) CountInFile(file int) (ret int) {
func CountInFile(cb Chessboard, file int) (ret int) {
if file < 1 || file > 8 {
return
}
Expand All @@ -32,7 +32,7 @@ func (cb Chessboard) CountInFile(file int) (ret int) {
}

// CountAll should count how many squares are present in the chessboard
func (cb Chessboard) CountAll() (ret int) {
func CountAll(cb Chessboard) (ret int) {
for _, rank := range cb {
for range rank {
ret++
Expand All @@ -42,9 +42,9 @@ func (cb Chessboard) CountAll() (ret int) {
}

// CountOccupied returns how many squares are occupied in the chessboard
func (cb Chessboard) CountOccupied() (ret int) {
func CountOccupied(cb Chessboard) (ret int) {
for rank := range cb {
ret += cb.CountInRank(rank)
ret += CountInRank(cb, rank)
}
return ret
}
14 changes: 6 additions & 8 deletions exercises/concept/chessboard/chessboard.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package chessboard

// Rank stores if a square is occupied by a piece
type Rank []bool
// Declare a type named Rank which stores if a square is occupied by a piece - this will be a slice of bools

// Chessboard contains eight Ranks, accessed with values from 'A' to 'H'
type Chessboard map[byte]Rank
// Declare a type named Chessboard contains a map of eight Ranks, accessed with values from 1 to 8

// CountInRank returns how many squares are occupied in the chessboard,
// within the given rank
func (cb Chessboard) CountInRank(rank byte) (ret int) {
func CountInRank(cb Chessboard, rank int) (ret int) {
panic("Please implement CountInRank()")
}

// CountInFile returns how many squares are occupied in the chessboard,
// within the given file
func (cb Chessboard) CountInFile(file int) (ret int) {
func CountInFile(cb Chessboard, file int) (ret int) {
panic("Please implement CountInFile()")
}

// CountAll should count how many squares are present in the chessboard
func (cb Chessboard) CountAll() (ret int) {
func CountAll(cb Chessboard) (ret int) {
panic("Please implement CountAll()")
}

// CountOccupied returns how many squares are occupied in the chessboard
func (cb Chessboard) CountOccupied() (ret int) {
func CountOccupied(cb Chessboard) (ret int) {
panic("Please implement CountOccupied()")
}
Loading