Skip to content

Commit f1773ad

Browse files
authored
Use any alias instead of interface{} (#276)
See golang/go#33232.
1 parent 9094ef9 commit f1773ad

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

cmp/internal/value/name.go

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strconv"
1010
)
1111

12+
var anyType = reflect.TypeOf((*interface{})(nil)).Elem()
13+
1214
// TypeString is nearly identical to reflect.Type.String,
1315
// but has an additional option to specify that full type names be used.
1416
func TypeString(t reflect.Type, qualified bool) string {
@@ -20,6 +22,11 @@ func appendTypeName(b []byte, t reflect.Type, qualified, elideFunc bool) []byte
2022
// of the same name and within the same package,
2123
// but declared within the namespace of different functions.
2224

25+
// Use the "any" alias instead of "interface{}" for better readability.
26+
if t == anyType {
27+
return append(b, "any"...)
28+
}
29+
2330
// Named type.
2431
if t.Name() != "" {
2532
if qualified && t.PkgPath() != "" {

cmp/internal/value/name_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestTypeString(t *testing.T) {
111111
want: "*$PackagePath.Named",
112112
}, {
113113
in: (*interface{})(nil),
114-
want: "*interface{}",
114+
want: "*any",
115115
}, {
116116
in: (*interface{ Read([]byte) (int, error) })(nil),
117117
want: "*interface{ Read([]uint8) (int, error) }",

cmp/testdata/diffs

+23-23
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
}
4747
>>> TestDiff/Comparer/DifferingHash
4848
<<< TestDiff/Comparer/NilStringer
49-
interface{}(
49+
any(
5050
- &fmt.Stringer(nil),
5151
)
5252
>>> TestDiff/Comparer/NilStringer
@@ -135,19 +135,19 @@
135135
)
136136
>>> TestDiff/Comparer/StringerMapKey
137137
<<< TestDiff/Comparer/StringerBacktick
138-
interface{}(
138+
any(
139139
- []*testprotos.Stringer{s`multi\nline\nline\nline`},
140140
)
141141
>>> TestDiff/Comparer/StringerBacktick
142142
<<< TestDiff/Comparer/DynamicMap
143-
[]interface{}{
144-
map[string]interface{}{
143+
[]any{
144+
map[string]any{
145145
"avg": float64(0.278),
146146
- "hr": int(65),
147147
+ "hr": float64(65),
148148
"name": string("Mark McGwire"),
149149
},
150-
map[string]interface{}{
150+
map[string]any{
151151
"avg": float64(0.288),
152152
- "hr": int(63),
153153
+ "hr": float64(63),
@@ -199,14 +199,14 @@
199199
}
200200
>>> TestDiff/Transformer/Filtered
201201
<<< TestDiff/Transformer/DisjointOutput
202-
int(Inverse(λ, interface{}(
202+
int(Inverse(λ, any(
203203
- string("zero"),
204204
+ float64(1),
205205
)))
206206
>>> TestDiff/Transformer/DisjointOutput
207207
<<< TestDiff/Transformer/JSON
208-
string(Inverse(ParseJSON, map[string]interface{}{
209-
"address": map[string]interface{}{
208+
string(Inverse(ParseJSON, map[string]any{
209+
"address": map[string]any{
210210
- "city": string("Los Angeles"),
211211
+ "city": string("New York"),
212212
"postalCode": string("10021-3100"),
@@ -215,18 +215,18 @@
215215
"streetAddress": string("21 2nd Street"),
216216
},
217217
"age": float64(25),
218-
"children": []interface{}{},
218+
"children": []any{},
219219
"firstName": string("John"),
220220
"isAlive": bool(true),
221221
"lastName": string("Smith"),
222-
"phoneNumbers": []interface{}{
223-
map[string]interface{}{
222+
"phoneNumbers": []any{
223+
map[string]any{
224224
- "number": string("212 555-4321"),
225225
+ "number": string("212 555-1234"),
226226
"type": string("home"),
227227
},
228-
map[string]interface{}{"number": string("646 555-4567"), "type": string("office")},
229-
map[string]interface{}{"number": string("123 456-7890"), "type": string("mobile")},
228+
map[string]any{"number": string("646 555-4567"), "type": string("office")},
229+
map[string]any{"number": string("123 456-7890"), "type": string("mobile")},
230230
},
231231
+ "spouse": nil,
232232
}))
@@ -267,7 +267,7 @@
267267
}
268268
>>> TestDiff/Reporter/PanicError
269269
<<< TestDiff/Reporter/AmbiguousType
270-
interface{}(
270+
any(
271271
- "github.com/google/go-cmp/cmp/internal/teststructs/foo1".Bar{},
272272
+ "github.com/google/go-cmp/cmp/internal/teststructs/foo2".Bar{},
273273
)
@@ -297,7 +297,7 @@
297297
}
298298
>>> TestDiff/Reporter/AmbiguousPointerMap
299299
<<< TestDiff/Reporter/AmbiguousStringer
300-
interface{}(
300+
any(
301301
- cmp_test.Stringer("hello"),
302302
+ &cmp_test.Stringer("hello"),
303303
)
@@ -327,7 +327,7 @@
327327
)
328328
>>> TestDiff/Reporter/AmbiguousSliceHeader
329329
<<< TestDiff/Reporter/AmbiguousStringerMapKey
330-
map[interface{}]string{
330+
map[any]string{
331331
- nil: "nil",
332332
+ &⟪0xdeadf00f⟫"github.com/google/go-cmp/cmp_test".Stringer("hello"): "goodbye",
333333
- "github.com/google/go-cmp/cmp_test".Stringer("hello"): "goodbye",
@@ -336,13 +336,13 @@
336336
}
337337
>>> TestDiff/Reporter/AmbiguousStringerMapKey
338338
<<< TestDiff/Reporter/NonAmbiguousStringerMapKey
339-
map[interface{}]string{
339+
map[any]string{
340340
+ s"fizz": "buzz",
341341
- s"hello": "goodbye",
342342
}
343343
>>> TestDiff/Reporter/NonAmbiguousStringerMapKey
344344
<<< TestDiff/Reporter/InvalidUTF8
345-
interface{}(
345+
any(
346346
- cmp_test.MyString("\xed\xa0\x80"),
347347
)
348348
>>> TestDiff/Reporter/InvalidUTF8
@@ -399,7 +399,7 @@
399399
}
400400
>>> TestDiff/Reporter/BatchedWithComparer
401401
<<< TestDiff/Reporter/BatchedLong
402-
interface{}(
402+
any(
403403
- cmp_test.MyComposite{IntsA: []int8{0, 1, 2, 3, 4, 5, 6, 7, ...}},
404404
)
405405
>>> TestDiff/Reporter/BatchedLong
@@ -1017,7 +1017,7 @@
10171017
}
10181018
>>> TestDiff/Reporter/LargeMapKey
10191019
<<< TestDiff/Reporter/LargeStringInInterface
1020-
struct{ X interface{} }{
1020+
struct{ X any }{
10211021
X: strings.Join({
10221022
... // 485 identical bytes
10231023
"s mus. Pellentesque mi lorem, consectetur id porttitor id, solli",
@@ -1028,7 +1028,7 @@
10281028
}
10291029
>>> TestDiff/Reporter/LargeStringInInterface
10301030
<<< TestDiff/Reporter/LargeBytesInInterface
1031-
struct{ X interface{} }{
1031+
struct{ X any }{
10321032
X: bytes.Join({
10331033
... // 485 identical bytes
10341034
"s mus. Pellentesque mi lorem, consectetur id porttitor id, solli",
@@ -1039,7 +1039,7 @@
10391039
}
10401040
>>> TestDiff/Reporter/LargeBytesInInterface
10411041
<<< TestDiff/Reporter/LargeStandaloneString
1042-
struct{ X interface{} }{
1042+
struct{ X any }{
10431043
- X: [1]string{
10441044
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet pretium ligula, at gravida quam. Integer iaculis, velit at sagittis ultricies, lacus metus scelerisque turpis, ornare feugiat nulla nisl ac erat. Maecenas elementum ultricies libero, sed efficitur lacus molestie non. Nulla ac pretium dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque mi lorem, consectetur id porttitor id, sollicitudin sit amet enim. Duis eu dolor magna. Nunc ut augue turpis.",
10451045
- },
@@ -1614,7 +1614,7 @@
16141614
... // 4 identical fields
16151615
ContSlaps: nil,
16161616
ContSlapsInterval: 0,
1617-
Animal: []interface{}{
1617+
Animal: []any{
16181618
teststructs.Goat{
16191619
Target: "corporation",
16201620
Slaps: nil,

0 commit comments

Comments
 (0)