[go] flag: use strconv instead of fmt in values' String funcs

53 views
Skip to first unread message

Ian Lance Taylor (Gerrit)

unread,
Sep 9, 2016, 3:57:43 PM9/9/16
to Albert Nigmatzianov, golang-co...@googlegroups.com
Ian Lance Taylor has posted comments on this change.

flag: use strconv instead of fmt in values' String funcs

Patch Set 1:

(1 comment)

https://go-review.googlesource.com/#/c/28914/1/src/flag/flag.go
File src/flag/flag.go:

Line 213: func (f *float64Value) String() string { return
strconv.FormatFloat(float64(*f), byte('f'), -1, 64) }
No need to explicitly convert to byte; 'f' is untyped.

More importantly, I'm pretty sure that fmt.Sprintf("%v", x) corresponds to
strconv.FormatFloat(x, 'g', -1, 64). That is, 'g', not 'f'.


--
https://go-review.googlesource.com/28914
Gerrit-HasComments: Yes

Albert Nigmatzianov (Gerrit)

unread,
Sep 10, 2016, 4:31:03 AM9/10/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Albert Nigmatzianov would like you to review this change by bogem:
https://go-review.googlesource.com/28914

flag: use strconv instead of fmt in values' String funcs

The existing implementation of flag values with fmt package uses
more memory and works slower than the implementation with strconv
package.

Change-Id: I9e749179f66d5c50cafe98186641bcdbc546d2db
---
M src/flag/flag.go
1 file changed, 7 insertions(+), 7 deletions(-)



diff --git a/src/flag/flag.go b/src/flag/flag.go
index e4705f2..54f1c0f 100644
--- a/src/flag/flag.go
+++ b/src/flag/flag.go
@@ -94,7 +94,7 @@

func (b *boolValue) Get() interface{} { return bool(*b) }

-func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
+func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }

func (b *boolValue) IsBoolFlag() bool { return true }

@@ -121,7 +121,7 @@

func (i *intValue) Get() interface{} { return int(*i) }

-func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
+func (i *intValue) String() string { return strconv.Itoa(int(*i)) }

// -- int64 Value
type int64Value int64
@@ -139,7 +139,7 @@

func (i *int64Value) Get() interface{} { return int64(*i) }

-func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
+func (i *int64Value) String() string { return strconv.FormatInt(int64(*i),
10) }

// -- uint Value
type uintValue uint
@@ -157,7 +157,7 @@

func (i *uintValue) Get() interface{} { return uint(*i) }

-func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
+func (i *uintValue) String() string { return
strconv.FormatUint(uint64(*i), 10) }

// -- uint64 Value
type uint64Value uint64
@@ -175,7 +175,7 @@

func (i *uint64Value) Get() interface{} { return uint64(*i) }

-func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
+func (i *uint64Value) String() string { return
strconv.FormatUint(uint64(*i), 10) }

// -- string Value
type stringValue string
@@ -192,7 +192,7 @@

func (s *stringValue) Get() interface{} { return string(*s) }

-func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
+func (s *stringValue) String() string { return string(*s) }

// -- float64 Value
type float64Value float64
@@ -210,7 +210,7 @@

func (f *float64Value) Get() interface{} { return float64(*f) }

-func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
+func (f *float64Value) String() string { return
strconv.FormatFloat(float64(*f), byte('f'), -1, 64) }

// -- time.Duration Value
type durationValue time.Duration

--
https://go-review.googlesource.com/28914

Albert Nigmatzianov (Gerrit)

unread,
Sep 10, 2016, 4:31:03 AM9/10/16
to golang-co...@googlegroups.com
Albert Nigmatzianov uploaded a new patch set:
https://go-review.googlesource.com/28914

flag: use strconv instead of fmt in values' String funcs

The existing implementation of flag values with fmt package uses
more memory and works slower than the implementation with strconv
package.

Change-Id: I9e749179f66d5c50cafe98186641bcdbc546d2db
---
M src/flag/flag.go
1 file changed, 7 insertions(+), 7 deletions(-)


--
https://go-review.googlesource.com/28914

Ian Lance Taylor (Gerrit)

unread,
Sep 10, 2016, 6:07:03 AM9/10/16
to Albert Nigmatzianov, golang-co...@googlegroups.com
Ian Lance Taylor has posted comments on this change.

flag: use strconv instead of fmt in values' String funcs

Patch Set 2: Run-TryBot+1 Code-Review+2

--
https://go-review.googlesource.com/28914
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-HasComments: No

Gobot Gobot (Gerrit)

unread,
Sep 10, 2016, 6:07:27 AM9/10/16
to Albert Nigmatzianov, Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

flag: use strconv instead of fmt in values' String funcs

Patch Set 2:

TryBots beginning. Status page: http://farmer.golang.org/try?commit=c7c06590

Gobot Gobot (Gerrit)

unread,
Sep 10, 2016, 6:18:38 AM9/10/16
to Albert Nigmatzianov, Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

flag: use strconv instead of fmt in values' String funcs

Patch Set 2: TryBot-Result+1

TryBots are happy.

--
https://go-review.googlesource.com/28914
Gerrit-Reviewer: Gobot Gobot <go...@golang.org>

Ian Lance Taylor (Gerrit)

unread,
Sep 10, 2016, 6:29:49 AM9/10/16
to Albert Nigmatzianov, golang-...@googlegroups.com, Gobot Gobot, golang-co...@googlegroups.com
Ian Lance Taylor has submitted this change and it was merged.

flag: use strconv instead of fmt in values' String funcs

The existing implementation of flag values with fmt package uses
more memory and works slower than the implementation with strconv
package.

Change-Id: I9e749179f66d5c50cafe98186641bcdbc546d2db
Reviewed-on: https://go-review.googlesource.com/28914
Reviewed-by: Ian Lance Taylor <ia...@golang.org>
Run-TryBot: Ian Lance Taylor <ia...@golang.org>
TryBot-Result: Gobot Gobot <go...@golang.org>
---
M src/flag/flag.go
1 file changed, 7 insertions(+), 7 deletions(-)

Approvals:
Ian Lance Taylor: Looks good to me, approved; Run TryBots
Gobot Gobot: TryBots succeeded
Reply all
Reply to author
Forward
0 new messages