Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zero_zilch_nada: remove ambiguous reference to empty #1500

Merged
merged 19 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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: 4 additions & 1 deletion concepts/nil/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"blurb": "nil is a special value in Go to represent the absence of a value.",
"authors": ["ErikSchierboom"],
"authors": [
"ErikSchierboom",
"sudomateo"
],
"contributors": []
}
59 changes: 38 additions & 21 deletions concepts/nil/about.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
# Zero Values
# Introduction
junedev marked this conversation as resolved.
Show resolved Hide resolved

Unlike some other languages, Go has no empty/null/undefined state for uninitialized variables. Every type has a [zero value][zero_values] that uninitialized variables will hold upon declaration.
Go does not have a concept of empty, null, or undefined for variable values. Variables declared without an explicit initial value default to the zero value for their respective type.

These default values are called the zero values for their respective types:
The zero value for primative types such as booleans, numeric types, and strings are `false`, `0`, and `""`, respectively.
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

The identifier `nil`, meaning zero, is the zero value for more complex types such as pointers, functions, interfaces, slices, channels, and maps.

The following table details the zero value for Go's types.

| Type | Zero Value |
| --------- | ---------- |
| bool | false |
| numeric | 0 |
| string | "" |
| pointer | nil |
| func | nil |
| interface | nil |
| slice | nil |
| channel | nil |
| map | nil |

The predeclared identifier `nil`, meaning zero, is the zero value for the more complex types in Go. Comparing a type whose zero value is not `nil` to `nil`, like string, is an error:
| boolean | `false` |
| numeric | `0` |
| string | `""` |
| pointer | `nil` |
| function | `nil` |
| interface | `nil` |
| slice | `nil` |
| channel | `nil` |
| map | `nil` |

## Comparing Types to Nil
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

Comparing a type whose zero value is not `nil` to `nil` is an error:
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

```go
func main() {
myString := "exercism"
if myString != nil { // invalid operation: myString != nil (mismatched types string and nil)
fmt.Println(myString)
}
}
func main() {
var myString string

if myString != nil { // invalid operation: myString != nil (mismatched types string and nil)
fmt.Println(myString)
}
}
```

[zero_values]: https://golang.org/ref/spec#The_zero_value
However, comparing a type whose zero value is `nil` to `nil` is acceptable:

```go
func main() {
var mySlice []int

if mySlice != nil {
fmt.Println(mySlice)
}
}
```
28 changes: 15 additions & 13 deletions concepts/nil/introduction.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# Introduction
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

In Go, uninitialized variables and their elements are given default values.
Go does not have a concept of empty, null, or undefined for variable values. Variables declared without an explicit initial value default to the zero value for their respective type.

These default values are called the zero values for their respective types:
The zero value for primative types such as booleans, numeric types, and strings are `false`, `0`, and `""`, respectively.
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

The identifier `nil`, meaning zero, is the zero value for more complex types such as pointers, functions, interfaces, slices, channels, and maps.

The following table details the zero value for Go's types.

| Type | Zero Value |
| --------- | ---------- |
| bool | false |
| numeric | 0 |
| string | "" |
| pointer | nil |
| func | nil |
| interface | nil |
| slice | nil |
| channel | nil |
| map | nil |

The identifier `nil`, meaning zero, is the zero value for the more complex types in Go.
| boolean | `false` |
| numeric | `0` |
| string | `""` |
| pointer | `nil` |
| function | `nil` |
| interface | `nil` |
| slice | `nil` |
| channel | `nil` |
| map | `nil` |
7 changes: 1 addition & 6 deletions concepts/nil/links.json
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
[
{
"url": "https://golang.org/ref/spec#The_zero_value",
"description": "zero_values"
}
]
[]
5 changes: 4 additions & 1 deletion concepts/zero-values/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"blurb": "When a variable is created without a value, it gets assigned the zero value of its type.",
"authors": ["ErikSchierboom"],
"authors": [
"ErikSchierboom",
"sudomateo"
],
"contributors": []
}
69 changes: 48 additions & 21 deletions concepts/zero-values/about.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
# Zero Values
# About

Unlike some other languages, Go has no empty/null/undefined state for uninitialized variables. Every type has a [zero value][zero_values] that uninitialized variables will hold upon declaration.
Go does not have a concept of empty, null, or undefined for variable values. Variables declared without an explicit initial value default to the zero value for their respective type.

These default values are called the zero values for their respective types:
The zero value for primative types such as booleans, numeric types, and strings are `false`, `0`, and `""`, respectively.

The identifier `nil`, meaning zero, is the zero value for more complex types such as pointers, functions, interfaces, slices, channels, and maps.

The following table details the zero value for Go's types.

| Type | Zero Value |
| --------- | ---------- |
| bool | false |
| numeric | 0 |
| string | "" |
| pointer | nil |
| func | nil |
| interface | nil |
| slice | nil |
| channel | nil |
| map | nil |

The predeclared identifier `nil`, meaning zero, is the zero value for the more complex types in Go. Comparing a type whose zero value is not `nil` to `nil`, like string, is an error:
| boolean | `false` |
| numeric | `0` |
| string | `""` |
| pointer | `nil` |
| function | `nil` |
| interface | `nil` |
| slice | `nil` |
| channel | `nil` |
| map | `nil` |
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

## Zero Value Construction

The `var` keyword can be used to construct any type to its zero value:

```go
func main() {
var myBool bool
fmt.Println("Zero value boolean:", myBool)
}
```
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

```go
func main() {
myString := "exercism"
if myString != nil { // invalid operation: myString != nil (mismatched types string and nil)
fmt.Println(myString)
}
}
func main() {
var mySlice []int
fmt.Println("Zero value slice:", mySlice)
}
```

[zero_values]: https://golang.org/ref/spec#The_zero_value
When constructing the zero value for a struct type, all of the struct's fields will be set to their zero value:

```go
type Person struct {
Name string
Age int
}

func main() {
var myPerson Person
fmt.Println("Zero value Person:", myPerson)
}
```

## Related Concepts

- [concept:go/nil]()
30 changes: 13 additions & 17 deletions concepts/zero-values/introduction.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
# Introduction

TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction
Go does not have a concept of empty, null, or undefined for variable values. Variables declared without an explicit initial value default to the zero value for their respective type.

## zero-value
The zero value for primative types such as booleans, numeric types, and strings are `false`, `0`, and `""`, respectively.

## nil
The identifier `nil`, meaning zero, is the zero value for more complex types such as pointers, functions, interfaces, slices, channels, and maps.

In Go, uninitialized variables and their elements are given default values.

These default values are called the zero values for their respective types:
The following table details the zero value for Go's types.

| Type | Zero Value |
| --------- | ---------- |
| bool | false |
| numeric | 0 |
| string | "" |
| pointer | nil |
| func | nil |
| interface | nil |
| slice | nil |
| channel | nil |
| map | nil |

The identifier `nil`, meaning zero, is the zero value for the more complex types in Go.
| boolean | `false` |
| numeric | `0` |
| string | `""` |
| pointer | `nil` |
| function | `nil` |
| interface | `nil` |
| slice | `nil` |
| channel | `nil` |
| map | `nil` |
7 changes: 1 addition & 6 deletions concepts/zero-values/links.json
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
[
{
"url": "https://golang.org/ref/spec#The_zero_value",
"description": "zero_values"
}
]
[]
sudomateo marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@
"status": "beta"
},
{
sudomateo marked this conversation as resolved.
Show resolved Hide resolved
"name": "Zero, Zilch, Nada",
"slug": "zero-zilch-nada",
"name": "Census",
"slug": "census",
"uuid": "fa4735f8-4d8a-4fb1-85ed-9cf03e9f7b7a",
"concepts": [
"nil",
Expand Down
20 changes: 20 additions & 0 deletions exercises/concept/census/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Hints

## 1. Create a new resident

- Revisit the [structs concept][concept-structs] for information on how to create structs.
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

## 2. Validate resident information

- The zero value for a string is `""`.
- The zero value for a map is `nil`.
- Revisit the [maps concept][concept-maps] for information on how to determine if a map key exists.

## 3. Delete resident information

- A resident is considered deleted when all of their fields are set to their zero value.
- Revisit the [zero-values concept][concept-zero-values] for the zero value for each Go type.

## 4. Count the residents

- Revisit the [range-iteration concept][concept-range-iteration] for information on how to range over a slice.
98 changes: 98 additions & 0 deletions exercises/concept/census/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Instructions
junedev marked this conversation as resolved.
Show resolved Hide resolved

It is your job to prepare the city's IT system for an upcoming census. Specifically, you are responsible for the program that will injest the data from the census workers.
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

The program must be able to create a new resident in the system when given a resident's information. Additionally, you will create functions that ensure the required information is present in the resident's data and delete a resident's data. Lastly, you will count the residents to provide an accurate census count.

## 1. Create a new resident

When a census worker collects a resident's information, they need register that resident by entering their name, age, and address into the system.
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

Implement the function `NewResident` that accepts three arguments:

- The name of the resident.

- The age of the resident.

- The address of the resident.
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

The function should return a pointer to a `Resident` struct that holds this information.

```go
name := "Matthew Sanabria"
age := 29
address := map[string]string{"street": "Main St.",}
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

NewResident(name, age, address)
// => &{Matthew Sanabria 29 map[street:Main St.]}
```

## 2. Validate required information

Residents may be reluctant to provide personal data to census workers. In those cases it's necessary to determine if the resident provided the required information to be counted in the census.

In order to be counted, a resident must provide a non-zero value for their name and an address that contains a non-zero value for the `street` key. All other information, such as the resident's age, is optional. Implement the `HasRequiredInfo` method that returns a boolean indicating if the resident has provided the required information.

```go
name := "Matthew Sanabria"
age := 0
address := make(map[string]string, 0)
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

resident := NewResident(name, age, address)

resident.HasRequiredInfo()
// => false
```

## 3. Delete resident information

Life moves fast and mistakes happen. A resident can move out of the city. A census worker can make mistakes when collecting data. In those cases, it's necessary to have the ability to delete a resident's data so they will not be counted.

Implement the `Delete` method that sets all of the fields the resident to their zero value.

```go
name := "Matthew Sanabria"
age := 29
address := map[string]string{"street": "Main St.",}

resident := NewResident(name, age, address)

fmt.Println(resident)
// => &{Matthew Sanabria 29 map[street:Main St.]}

resident.Delete()

fmt.Println(resident)
// => &{ 0 map[]}
```

## 4. Count the residents

Now that the system supports census data, it's time to perform the census and count the residents!

Implement the function `Count` that accepts one argument:

- A slice of pointers to `Resident` structs.

The function should return an integer indicating the number of residents that were counted in the census. A resident can only be counted if they provided the required information to the census worker.

```go
name1 := "Matthew Sanabria"
age1 := 29
address1 := map[string]string{"street": "Main St.",}

resident1 := NewResident(name1, age1, address1)

name2 := "Rob Pike"
age2 := 0
address2 := make(map[string]string, 0)

resident2 := NewResident(name2, age2, address2)

residents := []*Resident{resident1, resident2,}

Count(residents)
// => 1
}

```
Loading