Skip to content

math/big : Printf %s #24168

@Konstantin8105

Description

@Konstantin8105

What version of Go are you using (go version)?

go playground and go 1.8.3 linux/amd64

Does this issue reproduce with the latest release?

checked only for go playground and go 1.8.3 linux/amd64

What operating system and processor architecture are you using (go env)?

ubuntu linux x64

What did you do?

Playground:
https://play.golang.org/p/VNajWTsFQRd

Code:

package main

import (
	"fmt"
	"math/big"
)

type Val struct {
	a int
	b float64
}

func (v Val) String() string {
	return fmt.Sprintf("Val{%d,%v}", v.a, v.b)
}

func main() {
	// user struct
	vl := Val{12, 45}
	fmt.Printf("User struct => %s\n", vl)

	// big float
	f := big.NewFloat(23)
	fmt.Printf("Big Float 1 => %s\n", f)
	fmt.Printf("Big Float 2 => %s\n", (*f))
	fmt.Printf("Big Float 3 => %s\n", f.String())
	fmt.Printf("Big Float 4 => %s\n", (*f).String())
	fmt.Println("Big Float 5 => ", f)
}

Expected behavior:

  1. struct big.Float implemented interface Stringer from package fmt.
  2. In according to doc package fmt:
  1. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

What did you expect to see?

User struct => Val{12,45}
Big Float 1 => 23
Big Float 2 => 23
Big Float 3 => 23
Big Float 4 => 23
Big Float 5 =>  23

What did you see instead?

Result from golang platground:

User struct => Val{12,45}
Big Float 1 => %!s(*big.Float=23)
Big Float 2 => {%!s(uint32=53) %!s(big.RoundingMode=0) %!s(big.Accuracy=0) %!s(big.form=1) %!s(bool=false) [%!s(big.Word=0) %!s(big.Word=3087007744)] %!s(int32=5)}
Big Float 3 => 23
Big Float 4 => 23
Big Float 5 =>  23

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions