aboutsummaryrefslogtreecommitdiff
path: root/src/fmt
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-12-01 12:15:45 -0500
committerRuss Cox <rsc@golang.org>2021-12-13 18:45:54 +0000
commit2580d0e08d5e9f979b943758d3c49877fb2324cb (patch)
tree3aafccfd81087734156a1778ce2321adf345f271 /src/fmt
parent083ef5462494e81ee23316245c5d65085a3f62d9 (diff)
downloadgo-2580d0e08d5e9f979b943758d3c49877fb2324cb.tar.gz
go-2580d0e08d5e9f979b943758d3c49877fb2324cb.zip
all: gofmt -w -r 'interface{} -> any' src
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/fmt')
-rw-r--r--src/fmt/errors.go2
-rw-r--r--src/fmt/fmt_test.go50
-rw-r--r--src/fmt/print.go34
-rw-r--r--src/fmt/scan.go26
-rw-r--r--src/fmt/scan_test.go20
5 files changed, 66 insertions, 66 deletions
diff --git a/src/fmt/errors.go b/src/fmt/errors.go
index 466a620353..4f4daf19e1 100644
--- a/src/fmt/errors.go
+++ b/src/fmt/errors.go
@@ -14,7 +14,7 @@ import "errors"
// invalid to include more than one %w verb or to supply it with an operand
// that does not implement the error interface. The %w verb is otherwise
// a synonym for %v.
-func Errorf(format string, a ...interface{}) error {
+func Errorf(format string, a ...any) error {
p := newPrinter()
p.wrapErrs = true
p.doPrintf(format, a)
diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go
index 87fb323809..a4c65b8f5e 100644
--- a/src/fmt/fmt_test.go
+++ b/src/fmt/fmt_test.go
@@ -40,7 +40,7 @@ type (
)
func TestFmtInterface(t *testing.T) {
- var i1 interface{}
+ var i1 any
i1 = "abc"
s := Sprintf("%s", i1)
if s != "abc" {
@@ -56,7 +56,7 @@ var (
intVar = 0
array = [5]int{1, 2, 3, 4, 5}
- iarray = [4]interface{}{1, "hello", 2.5, nil}
+ iarray = [4]any{1, "hello", 2.5, nil}
slice = array[:]
islice = iarray[:]
)
@@ -100,7 +100,7 @@ type S struct {
}
type SI struct {
- I interface{}
+ I any
}
// P is a type with a String method with pointer receiver for testing %p.
@@ -141,7 +141,7 @@ func (sf writeStringFormatter) Format(f State, c rune) {
var fmtTests = []struct {
fmt string
- val interface{}
+ val any
out string
}{
{"%d", 12345, "12345"},
@@ -993,14 +993,14 @@ var fmtTests = []struct {
// float and complex formatting should not change the padding width
// for other elements. See issue 14642.
- {"%06v", []interface{}{+10.0, 10}, "[000010 000010]"},
- {"%06v", []interface{}{-10.0, 10}, "[-00010 000010]"},
- {"%06v", []interface{}{+10.0 + 10i, 10}, "[(000010+00010i) 000010]"},
- {"%06v", []interface{}{-10.0 + 10i, 10}, "[(-00010+00010i) 000010]"},
+ {"%06v", []any{+10.0, 10}, "[000010 000010]"},
+ {"%06v", []any{-10.0, 10}, "[-00010 000010]"},
+ {"%06v", []any{+10.0 + 10i, 10}, "[(000010+00010i) 000010]"},
+ {"%06v", []any{-10.0 + 10i, 10}, "[(-00010+00010i) 000010]"},
// integer formatting should not alter padding for other elements.
- {"%03.6v", []interface{}{1, 2.0, "x"}, "[000001 002 00x]"},
- {"%03.0v", []interface{}{0, 2.0, "x"}, "[ 002 000]"},
+ {"%03.6v", []any{1, 2.0, "x"}, "[000001 002 00x]"},
+ {"%03.0v", []any{0, 2.0, "x"}, "[ 002 000]"},
// Complex fmt used to leave the plus flag set for future entries in the array
// causing +2+0i and +3+0i instead of 2+0i and 3+0i.
@@ -1060,7 +1060,7 @@ var fmtTests = []struct {
// Tests to check that not supported verbs generate an error string.
{"%☠", nil, "%!☠(<nil>)"},
- {"%☠", interface{}(nil), "%!☠(<nil>)"},
+ {"%☠", any(nil), "%!☠(<nil>)"},
{"%☠", int(0), "%!☠(int=0)"},
{"%☠", uint(0), "%!☠(uint=0)"},
{"%☠", []byte{0, 1}, "[%!☠(uint8=0) %!☠(uint8=1)]"},
@@ -1077,8 +1077,8 @@ var fmtTests = []struct {
{"%☠", func() {}, "%!☠(func()=0xPTR)"},
{"%☠", reflect.ValueOf(renamedInt(0)), "%!☠(fmt_test.renamedInt=0)"},
{"%☠", SI{renamedInt(0)}, "{%!☠(fmt_test.renamedInt=0)}"},
- {"%☠", &[]interface{}{I(1), G(2)}, "&[%!☠(fmt_test.I=1) %!☠(fmt_test.G=2)]"},
- {"%☠", SI{&[]interface{}{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"},
+ {"%☠", &[]any{I(1), G(2)}, "&[%!☠(fmt_test.I=1) %!☠(fmt_test.G=2)]"},
+ {"%☠", SI{&[]any{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"},
{"%☠", reflect.Value{}, "<invalid reflect.Value>"},
{"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(int=1)]"},
}
@@ -1180,7 +1180,7 @@ func TestComplexFormatting(t *testing.T) {
}
}
-type SE []interface{} // slice of empty; notational compactness.
+type SE []any // slice of empty; notational compactness.
var reorderTests = []struct {
fmt string
@@ -1267,7 +1267,7 @@ func BenchmarkSprintfTruncateString(b *testing.B) {
}
func BenchmarkSprintfTruncateBytes(b *testing.B) {
- var bytes interface{} = []byte("日本語日本語日本語日本語")
+ var bytes any = []byte("日本語日本語日本語日本語")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Sprintf("%.3s", bytes)
@@ -1375,7 +1375,7 @@ func BenchmarkSprintfStringer(b *testing.B) {
}
func BenchmarkSprintfStructure(b *testing.B) {
- s := &[]interface{}{SI{12345}, map[int]string{0: "hello"}}
+ s := &[]any{SI{12345}, map[int]string{0: "hello"}}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Sprintf("%#v", s)
@@ -1411,7 +1411,7 @@ func BenchmarkFprintfBytes(b *testing.B) {
}
func BenchmarkFprintIntNoAlloc(b *testing.B) {
- var x interface{} = 123456
+ var x any = 123456
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
buf.Reset()
@@ -1641,11 +1641,11 @@ func TestFormatterPrintln(t *testing.T) {
}
}
-func args(a ...interface{}) []interface{} { return a }
+func args(a ...any) []any { return a }
var startests = []struct {
fmt string
- in []interface{}
+ in []any
out string
}{
{"%*d", args(4, 42), " 42"},
@@ -1687,7 +1687,7 @@ func TestWidthAndPrecision(t *testing.T) {
// PanicS is a type that panics in String.
type PanicS struct {
- message interface{}
+ message any
}
// Value receiver.
@@ -1697,7 +1697,7 @@ func (p PanicS) String() string {
// PanicGo is a type that panics in GoString.
type PanicGo struct {
- message interface{}
+ message any
}
// Value receiver.
@@ -1707,7 +1707,7 @@ func (p PanicGo) GoString() string {
// PanicF is a type that panics in Format.
type PanicF struct {
- message interface{}
+ message any
}
// Value receiver.
@@ -1717,7 +1717,7 @@ func (p PanicF) Format(f State, c rune) {
var panictests = []struct {
fmt string
- in interface{}
+ in any
out string
}{
// String
@@ -1729,7 +1729,7 @@ var panictests = []struct {
{"%#v", PanicGo{io.ErrUnexpectedEOF}, "%!v(PANIC=GoString method: unexpected EOF)"},
{"%#v", PanicGo{3}, "%!v(PANIC=GoString method: 3)"},
// Issue 18282. catchPanic should not clear fmtFlags permanently.
- {"%#v", []interface{}{PanicGo{3}, PanicGo{3}}, "[]interface {}{%!v(PANIC=GoString method: 3), %!v(PANIC=GoString method: 3)}"},
+ {"%#v", []any{PanicGo{3}, PanicGo{3}}, "[]interface {}{%!v(PANIC=GoString method: 3), %!v(PANIC=GoString method: 3)}"},
// Format
{"%s", (*PanicF)(nil), "<nil>"}, // nil pointer special case
{"%s", PanicF{io.ErrUnexpectedEOF}, "%!s(PANIC=Format method: unexpected EOF)"},
@@ -1805,7 +1805,7 @@ func TestNilDoesNotBecomeTyped(t *testing.T) {
var formatterFlagTests = []struct {
in string
- val interface{}
+ val any
out string
}{
// scalar values with the (unused by fmt) 'a' verb.
diff --git a/src/fmt/print.go b/src/fmt/print.go
index 698ab557a4..1c37c3cb7b 100644
--- a/src/fmt/print.go
+++ b/src/fmt/print.go
@@ -106,7 +106,7 @@ type pp struct {
buf buffer
// arg holds the current item, as an interface{}.
- arg interface{}
+ arg any
// value is used instead of arg for reflect values.
value reflect.Value
@@ -129,7 +129,7 @@ type pp struct {
}
var ppFree = sync.Pool{
- New: func() interface{} { return new(pp) },
+ New: func() any { return new(pp) },
}
// newPrinter allocates a new pp struct or grabs a cached one.
@@ -199,7 +199,7 @@ func (p *pp) WriteString(s string) (ret int, err error) {
// Fprintf formats according to a format specifier and writes to w.
// It returns the number of bytes written and any write error encountered.
-func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
+func Fprintf(w io.Writer, format string, a ...any) (n int, err error) {
p := newPrinter()
p.doPrintf(format, a)
n, err = w.Write(p.buf)
@@ -209,12 +209,12 @@ func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
-func Printf(format string, a ...interface{}) (n int, err error) {
+func Printf(format string, a ...any) (n int, err error) {
return Fprintf(os.Stdout, format, a...)
}
// Sprintf formats according to a format specifier and returns the resulting string.
-func Sprintf(format string, a ...interface{}) string {
+func Sprintf(format string, a ...any) string {
p := newPrinter()
p.doPrintf(format, a)
s := string(p.buf)
@@ -227,7 +227,7 @@ func Sprintf(format string, a ...interface{}) string {
// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
-func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
+func Fprint(w io.Writer, a ...any) (n int, err error) {
p := newPrinter()
p.doPrint(a)
n, err = w.Write(p.buf)
@@ -238,13 +238,13 @@ func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
-func Print(a ...interface{}) (n int, err error) {
+func Print(a ...any) (n int, err error) {
return Fprint(os.Stdout, a...)
}
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
-func Sprint(a ...interface{}) string {
+func Sprint(a ...any) string {
p := newPrinter()
p.doPrint(a)
s := string(p.buf)
@@ -259,7 +259,7 @@ func Sprint(a ...interface{}) string {
// Fprintln formats using the default formats for its operands and writes to w.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
-func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
+func Fprintln(w io.Writer, a ...any) (n int, err error) {
p := newPrinter()
p.doPrintln(a)
n, err = w.Write(p.buf)
@@ -270,13 +270,13 @@ func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
-func Println(a ...interface{}) (n int, err error) {
+func Println(a ...any) (n int, err error) {
return Fprintln(os.Stdout, a...)
}
// Sprintln formats using the default formats for its operands and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
-func Sprintln(a ...interface{}) string {
+func Sprintln(a ...any) string {
p := newPrinter()
p.doPrintln(a)
s := string(p.buf)
@@ -533,7 +533,7 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune) {
}
}
-func (p *pp) catchPanic(arg interface{}, verb rune, method string) {
+func (p *pp) catchPanic(arg any, verb rune, method string) {
if err := recover(); err != nil {
// If it's a nil pointer, just say "<nil>". The likeliest causes are a
// Stringer that fails to guard against nil or a nil pointer for a
@@ -631,7 +631,7 @@ func (p *pp) handleMethods(verb rune) (handled bool) {
return false
}
-func (p *pp) printArg(arg interface{}, verb rune) {
+func (p *pp) printArg(arg any, verb rune) {
p.arg = arg
p.value = reflect.Value{}
@@ -886,7 +886,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
}
// intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type.
-func intFromArg(a []interface{}, argNum int) (num int, isInt bool, newArgNum int) {
+func intFromArg(a []any, argNum int) (num int, isInt bool, newArgNum int) {
newArgNum = argNum
if argNum < len(a) {
num, isInt = a[argNum].(int) // Almost always OK.
@@ -971,7 +971,7 @@ func (p *pp) missingArg(verb rune) {
p.buf.writeString(missingString)
}
-func (p *pp) doPrintf(format string, a []interface{}) {
+func (p *pp) doPrintf(format string, a []any) {
end := len(format)
argNum := 0 // we process one argument per non-trivial format
afterIndex := false // previous item in format was an index like [3].
@@ -1146,7 +1146,7 @@ formatLoop:
}
}
-func (p *pp) doPrint(a []interface{}) {
+func (p *pp) doPrint(a []any) {
prevString := false
for argNum, arg := range a {
isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
@@ -1161,7 +1161,7 @@ func (p *pp) doPrint(a []interface{}) {
// doPrintln is like doPrint but always adds a space between arguments
// and a newline after the last argument.
-func (p *pp) doPrintln(a []interface{}) {
+func (p *pp) doPrintln(a []any) {
for argNum, arg := range a {
if argNum > 0 {
p.buf.writeByte(' ')
diff --git a/src/fmt/scan.go b/src/fmt/scan.go
index 18cb608f43..d38610df35 100644
--- a/src/fmt/scan.go
+++ b/src/fmt/scan.go
@@ -60,13 +60,13 @@ type Scanner interface {
// space-separated values into successive arguments. Newlines count
// as space. It returns the number of items successfully scanned.
// If that is less than the number of arguments, err will report why.
-func Scan(a ...interface{}) (n int, err error) {
+func Scan(a ...any) (n int, err error) {
return Fscan(os.Stdin, a...)
}
// Scanln is similar to Scan, but stops scanning at a newline and
// after the final item there must be a newline or EOF.
-func Scanln(a ...interface{}) (n int, err error) {
+func Scanln(a ...any) (n int, err error) {
return Fscanln(os.Stdin, a...)
}
@@ -77,7 +77,7 @@ func Scanln(a ...interface{}) (n int, err error) {
// Newlines in the input must match newlines in the format.
// The one exception: the verb %c always scans the next rune in the
// input, even if it is a space (or tab etc.) or newline.
-func Scanf(format string, a ...interface{}) (n int, err error) {
+func Scanf(format string, a ...any) (n int, err error) {
return Fscanf(os.Stdin, format, a...)
}
@@ -96,13 +96,13 @@ func (r *stringReader) Read(b []byte) (n int, err error) {
// values into successive arguments. Newlines count as space. It
// returns the number of items successfully scanned. If that is less
// than the number of arguments, err will report why.
-func Sscan(str string, a ...interface{}) (n int, err error) {
+func Sscan(str string, a ...any) (n int, err error) {
return Fscan((*stringReader)(&str), a...)
}
// Sscanln is similar to Sscan, but stops scanning at a newline and
// after the final item there must be a newline or EOF.
-func Sscanln(str string, a ...interface{}) (n int, err error) {
+func Sscanln(str string, a ...any) (n int, err error) {
return Fscanln((*stringReader)(&str), a...)
}
@@ -110,7 +110,7 @@ func Sscanln(str string, a ...interface{}) (n int, err error) {
// values into successive arguments as determined by the format. It
// returns the number of items successfully parsed.
// Newlines in the input must match newlines in the format.
-func Sscanf(str string, format string, a ...interface{}) (n int, err error) {
+func Sscanf(str string, format string, a ...any) (n int, err error) {
return Fscanf((*stringReader)(&str), format, a...)
}
@@ -118,7 +118,7 @@ func Sscanf(str string, format string, a ...interface{}) (n int, err error) {
// values into successive arguments. Newlines count as space. It
// returns the number of items successfully scanned. If that is less
// than the number of arguments, err will report why.
-func Fscan(r io.Reader, a ...interface{}) (n int, err error) {
+func Fscan(r io.Reader, a ...any) (n int, err error) {
s, old := newScanState(r, true, false)
n, err = s.doScan(a)
s.free(old)
@@ -127,7 +127,7 @@ func Fscan(r io.Reader, a ...interface{}) (n int, err error) {
// Fscanln is similar to Fscan, but stops scanning at a newline and
// after the final item there must be a newline or EOF.
-func Fscanln(r io.Reader, a ...interface{}) (n int, err error) {
+func Fscanln(r io.Reader, a ...any) (n int, err error) {
s, old := newScanState(r, false, true)
n, err = s.doScan(a)
s.free(old)
@@ -138,7 +138,7 @@ func Fscanln(r io.Reader, a ...interface{}) (n int, err error) {
// values into successive arguments as determined by the format. It
// returns the number of items successfully parsed.
// Newlines in the input must match newlines in the format.
-func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error) {
+func Fscanf(r io.Reader, format string, a ...any) (n int, err error) {
s, old := newScanState(r, false, false)
n, err = s.doScanf(format, a)
s.free(old)
@@ -376,7 +376,7 @@ func (r *readRune) UnreadRune() error {
}
var ssFree = sync.Pool{
- New: func() interface{} { return new(ss) },
+ New: func() any { return new(ss) },
}
// newScanState allocates a new ss struct or grab a cached one.
@@ -950,7 +950,7 @@ func (s *ss) scanPercent() {
}
// scanOne scans a single value, deriving the scanner from the type of the argument.
-func (s *ss) scanOne(verb rune, arg interface{}) {
+func (s *ss) scanOne(verb rune, arg any) {
s.buf = s.buf[:0]
var err error
// If the parameter has its own Scan method, use that.
@@ -1067,7 +1067,7 @@ func errorHandler(errp *error) {
}
// doScan does the real work for scanning without a format string.
-func (s *ss) doScan(a []interface{}) (numProcessed int, err error) {
+func (s *ss) doScan(a []any) (numProcessed int, err error) {
defer errorHandler(&err)
for _, arg := range a {
s.scanOne('v', arg)
@@ -1178,7 +1178,7 @@ func (s *ss) advance(format string) (i int) {
// doScanf does the real work when scanning with a format string.
// At the moment, it handles only pointers to basic types.
-func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err error) {
+func (s *ss) doScanf(format string, a []any) (numProcessed int, err error) {
defer errorHandler(&err)
end := len(format) - 1
// We process one item per non-trivial format
diff --git a/src/fmt/scan_test.go b/src/fmt/scan_test.go
index 6b71b792ed..da0dfd19a2 100644
--- a/src/fmt/scan_test.go
+++ b/src/fmt/scan_test.go
@@ -21,22 +21,22 @@ import (
type ScanTest struct {
text string
- in interface{}
- out interface{}
+ in any
+ out any
}
type ScanfTest struct {
format string
text string
- in interface{}
- out interface{}
+ in any
+ out any
}
type ScanfMultiTest struct {
format string
text string
- in []interface{}
- out []interface{}
+ in []any
+ out []any
err string
}
@@ -444,7 +444,7 @@ var z IntString
var r1, r2, r3 rune
var multiTests = []ScanfMultiTest{
- {"", "", []interface{}{}, []interface{}{}, ""},
+ {"", "", []any{}, []any{}, ""},
{"%d", "23", args(&i), args(23), ""},
{"%2s%3s", "22333", args(&s, &t), args("22", "333"), ""},
{"%2d%3d", "44555", args(&i, &j), args(44, 555), ""},
@@ -498,7 +498,7 @@ var readers = []struct {
}},
}
-func testScan(t *testing.T, f func(string) io.Reader, scan func(r io.Reader, a ...interface{}) (int, error)) {
+func testScan(t *testing.T, f func(string) io.Reader, scan func(r io.Reader, a ...any) (int, error)) {
for _, test := range scanTests {
r := f(test.text)
n, err := scan(r, test.in)
@@ -637,7 +637,7 @@ func TestInf(t *testing.T) {
}
func testScanfMulti(t *testing.T, f func(string) io.Reader) {
- sliceType := reflect.TypeOf(make([]interface{}, 1))
+ sliceType := reflect.TypeOf(make([]any, 1))
for _, test := range multiTests {
r := f(test.text)
n, err := Fscanf(r, test.format, test.in...)
@@ -836,7 +836,7 @@ func TestEOFAtEndOfInput(t *testing.T) {
var eofTests = []struct {
format string
- v interface{}
+ v any
}{
{"%s", &stringVal},
{"%q", &stringVal},