mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-15 01:31:17 +00:00
first
This commit is contained in:
51
course/11-pointers/exercises/4-nil_dereference/code.go
Normal file
51
course/11-pointers/exercises/4-nil_dereference/code.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func removeProfanity(message *string) {
|
||||
// ?
|
||||
messageVal := *message
|
||||
messageVal = strings.ReplaceAll(messageVal, "dang", "****")
|
||||
messageVal = strings.ReplaceAll(messageVal, "shoot", "*****")
|
||||
messageVal = strings.ReplaceAll(messageVal, "heck", "****")
|
||||
*message = messageVal
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(messages []string) {
|
||||
for _, message := range messages {
|
||||
if message == "" {
|
||||
removeProfanity(nil)
|
||||
fmt.Println("nil message detected")
|
||||
} else {
|
||||
removeProfanity(&message)
|
||||
fmt.Println(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
messages := []string{
|
||||
"well shoot, this is awful",
|
||||
"",
|
||||
"dang robots",
|
||||
"dang them to heck",
|
||||
"",
|
||||
}
|
||||
|
||||
messages2 := []string{
|
||||
"well shoot",
|
||||
"",
|
||||
"Allan is going straight to heck",
|
||||
"dang... that's a tough break",
|
||||
"",
|
||||
}
|
||||
|
||||
test(messages)
|
||||
test(messages2)
|
||||
|
||||
}
|
||||
53
course/11-pointers/exercises/4-nil_dereference/complete.go
Normal file
53
course/11-pointers/exercises/4-nil_dereference/complete.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func removeProfanity(message *string) {
|
||||
if message == nil {
|
||||
return
|
||||
}
|
||||
messageVal := *message
|
||||
messageVal = strings.ReplaceAll(messageVal, "dang", "****")
|
||||
messageVal = strings.ReplaceAll(messageVal, "shoot", "*****")
|
||||
messageVal = strings.ReplaceAll(messageVal, "heck", "****")
|
||||
*message = messageVal
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(messages []string) {
|
||||
for _, message := range messages {
|
||||
if message == "" {
|
||||
removeProfanity(nil)
|
||||
fmt.Println("nil message detected")
|
||||
} else {
|
||||
removeProfanity(&message)
|
||||
fmt.Println(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
messages := []string{
|
||||
"well shoot, this is awful",
|
||||
"",
|
||||
"dang robots",
|
||||
"dang them to heck",
|
||||
"",
|
||||
}
|
||||
|
||||
messages2 := []string{
|
||||
"well shoot",
|
||||
"",
|
||||
"Allan is going straight to heck",
|
||||
"dang... that's a tough break",
|
||||
"",
|
||||
}
|
||||
|
||||
test(messages)
|
||||
test(messages2)
|
||||
|
||||
}
|
||||
10
course/11-pointers/exercises/4-nil_dereference/expected.txt
Normal file
10
course/11-pointers/exercises/4-nil_dereference/expected.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
well *****, this is awful
|
||||
nil message detected
|
||||
**** robots
|
||||
**** them to ****
|
||||
nil message detected
|
||||
well *****
|
||||
nil message detected
|
||||
Allan is going straight to ****
|
||||
****... that's a tough break
|
||||
nil message detected
|
||||
9
course/11-pointers/exercises/4-nil_dereference/readme.md
Normal file
9
course/11-pointers/exercises/4-nil_dereference/readme.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Nil Pointers
|
||||
|
||||
Pointers can be very dangerous.
|
||||
|
||||
If a pointer points to nothing (the zero value of the pointer type) then dereferencing it will cause a runtime error (a [panic](https://gobyexample.com/panic)) that crashes the program. Generally speaking, whenever you're dealing with pointers you should check if it's `nil` before trying to dereference it.
|
||||
|
||||
## Assignment
|
||||
|
||||
Let's make our profanity checker *safe*. Update the `removeProfanity` function. If `message` is `nil`, `return` early to avoid a [panic](https://gobyexample.com/panic). After all, there are no bad words to remove.
|
||||
Reference in New Issue
Block a user