mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-14 01:01:16 +00:00
first
This commit is contained in:
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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user