Skip to content

Commit 838c9d4

Browse files
authored
refactor: use any instead of interface{} (#2886)
[no important files changed]
1 parent 1cebcbd commit 838c9d4

87 files changed

Lines changed: 213 additions & 213 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

exercises/concept/airport-robot/.docs/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ It only needs to have all the necessary methods defined.
6767

6868
There is one very special interface type in Go, the **empty interface** type that contains zero methods.
6969
The empty interface is written like this: `interface{}`.
70-
In Go 1.18 or higher, `any` can be used as well. It was defined as an alias.
70+
Since Go 1.18, `any` is an alias for `interface{}`, and is more commonly used.
7171

7272
Since the empty interface has no methods, every type implements it implicitly.
7373
This is helpful for defining a function that can generically accept any value.

exercises/concept/sorting-room/.docs/instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ NOTE: we should use the `ExtractFancyNumber` function!
6161

6262
## 5. Implement `DescribeAnything` which uses them all
6363

64-
This is the main function Jen needs which takes any input (the empty interface means any value at all: `interface{}`).
64+
This is the main function Jen needs which takes any input.
6565
`DescribeAnything` should delegate to the other functions based on the type of the value passed in.
6666
More specifically:
6767

exercises/concept/sorting-room/.docs/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ A type assertion allows us to extract the interface value's underlying concrete
3333
For example:
3434

3535
```go
36-
var input interface{} = 12
36+
var input any = 12
3737
number := input.(int)
3838
```
3939

@@ -57,7 +57,7 @@ It has the same syntax as a type assertion (`interfaceVariable.(concreteType)`),
5757
Here is an example:
5858

5959
```go
60-
var i interface{} = 12 // try: 12.3, true, int64(12), []int{}, map[string]int{}
60+
var i any = 12 // try: 12.3, true, int64(12), []int{}, map[string]int{}
6161

6262
switch v := i.(type) {
6363
case int:

exercises/concept/sorting-room/.meta/exemplar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func DescribeFancyNumberBox(fnb FancyNumberBox) string {
5252
}
5353

5454
// DescribeAnything should return a string describing whatever it contains.
55-
func DescribeAnything(i interface{}) string {
55+
func DescribeAnything(i any) string {
5656
switch v := i.(type) {
5757
case int:
5858
return DescribeNumber(float64(v))

exercises/concept/sorting-room/sorting_room.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ func DescribeFancyNumberBox(fnb FancyNumberBox) string {
3838
}
3939

4040
// DescribeAnything should return a string describing whatever it contains.
41-
func DescribeAnything(i interface{}) string {
41+
func DescribeAnything(i any) string {
4242
panic("Please implement DescribeAnything")
4343
}

exercises/concept/sorting-room/sorting_room_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestDescribeFancyNumberBox(t *testing.T) {
155155
func TestDescribeAnything(t *testing.T) {
156156
tests := []struct {
157157
description string
158-
input interface{}
158+
input any
159159
want string
160160
}{
161161
{

exercises/practice/acronym/.meta/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func main() {
1111
if err != nil {
1212
log.Fatal(err)
1313
}
14-
j := map[string]interface{}{
14+
j := map[string]any{
1515
"abbreviate": &[]testCase{},
1616
}
1717
if err := gen.Gen("acronym", j, t); err != nil {

exercises/practice/all-your-base/.meta/gen.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func main() {
1111
if err != nil {
1212
log.Fatal(err)
1313
}
14-
j := map[string]interface{}{
14+
j := map[string]any{
1515
"rebase": &[]testCase{},
1616
}
1717
if err := gen.Gen("all-your-base", j, t); err != nil {
@@ -26,11 +26,11 @@ type testCase struct {
2626
Digits []int `json:"digits"`
2727
OutputBase int `json:"outputBase"`
2828
} `json:"input"`
29-
Expected interface{} `json:"expected"`
29+
Expected any `json:"expected"`
3030
}
3131

3232
func (o testCase) Result() []int {
33-
s, ok := o.Expected.([]interface{})
33+
s, ok := o.Expected.([]any)
3434
if !ok {
3535
return nil
3636
}
@@ -43,7 +43,7 @@ func (o testCase) Result() []int {
4343
}
4444

4545
func (o testCase) Err() string {
46-
m, ok := o.Expected.(map[string]interface{})
46+
m, ok := o.Expected.(map[string]any)
4747
if !ok {
4848
return ""
4949
}

exercises/practice/allergies/.meta/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func main() {
1111
if err != nil {
1212
log.Fatal(err)
1313
}
14-
j := map[string]interface{}{
14+
j := map[string]any{
1515
"allergicTo": &[]allergicToCase{},
1616
"list": &[]listCase{},
1717
}

exercises/practice/alphametics/.meta/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
if err != nil {
1414
log.Fatal(err)
1515
}
16-
j := map[string]interface{}{
16+
j := map[string]any{
1717
"solve": &[]testCase{},
1818
}
1919
if err := gen.Gen("alphametics", j, t); err != nil {

0 commit comments

Comments
 (0)