-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(gnolang): add challenges (#765)
- Loading branch information
Showing
7 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
func main() { | ||
m := map[string]string{ | ||
"hello": "foo", | ||
"world": "bar", | ||
"hello": "", | ||
} | ||
} | ||
|
||
// Error: | ||
// duplicate key "hello" in map literal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
func main() { | ||
f() | ||
} | ||
|
||
func f() { | ||
defer func() { | ||
panic("second") | ||
}() | ||
panic("first") | ||
} | ||
|
||
// Error: | ||
// first | ||
// second |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
func main() { | ||
f() | ||
} | ||
|
||
func f() { | ||
defer func() { println("f recover", recover()) }() | ||
defer g() | ||
panic("wtf") | ||
} | ||
|
||
func g() { | ||
defer func() { | ||
// g() shouldn't be able to recover from f()'s panic, because the recover | ||
// is declared in a deferred closure that is absent from the stack at the | ||
// time of the panic. | ||
// See go behavior here https://go.dev/play/p/CcMGgY606O- | ||
// See Rob's examples https://groups.google.com/g/golang-nuts/c/HOXNBQu5c-Q/m/Je0qo1hbxIsJ | ||
println("g recover", recover()) | ||
}() | ||
} | ||
|
||
// Output: | ||
// g recover undefined | ||
// f recover wtf | ||
// false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package main | ||
|
||
func main() { | ||
_ = []string{0: "foo", 2: "bar", 2: "baz"} | ||
} | ||
|
||
// Error: | ||
// duplicate index 2 in array or slice literal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
defer func() { | ||
recover() | ||
panic("other panic") | ||
}() | ||
panic("test panic") | ||
} | ||
|
||
// Error: | ||
// other panic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
func main() { | ||
f() | ||
} | ||
|
||
func f() { | ||
defer func() { println("f recover", recover()) }() | ||
defer g() | ||
panic("wtf") | ||
} | ||
|
||
func g() { | ||
println("g recover", recover()) | ||
} | ||
|
||
// Output: | ||
// g recover wtf | ||
// f recover undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
func main() { | ||
println(f(false)) | ||
println(f(true)) | ||
} | ||
|
||
func f(dopanic bool) (err error) { | ||
defer func() { | ||
if x := recover(); x != nil { | ||
err = x.(error) | ||
} | ||
}() | ||
q(dopanic) | ||
return | ||
} | ||
|
||
func q(dopanic bool) { | ||
if dopanic { | ||
panic(errors.New("wtf")) | ||
} | ||
} | ||
|
||
// Output: | ||
// undefined | ||
// wtf |