Why no error shows up when I compile a float and try to print with %d?

Hi,

Second day of learning Go and having some fun. I’m on Windows 7 (64 Bit) with go version go1.7.3 windows/amd64.
Kindly note:- I am not trying to point out mistakes or something, just trying to understand it. I’m not a programmer/developer. So if I end up asking something “noobish”, kindly pardon me.

I have the below script:



package main

import (
        "fmt"
)
func main () {
        power:= 234.735
        name := "Pritesh Ugrankar"
        fmt.Printf("My name is %s, and my power is, %d", name, power)
}

Then I built it so:

C:\Users\pritesh\gogo>go build go4.go
C:\Users\pritesh\gogo>

Notice, no errors thrown.

And then I executed it:

C:\Users\pritesh\gogo>go4.exe
My name is Pritesh Ugrankar, and my power is, %!d(float64=234.735)

I didn’t understand why it says %!d(float64=234.735). May be that is Go’s way of saying "Hey, you used a decimal when you had a float. why would you do that?"
But I thought it would be caught during compile time.

So I changed it to this:

C:\Users\pritesh\gogo>more go4.go
package main

import (
        "fmt"
)
func main () {
        power:= 234.735
        name := "Pritesh Ugrankar"
        fmt.Printf("My name is %s, and my power is, %f", name, power)
}

And built it again.

C:\Users\pritesh\gogo>go build go4.go
C:\Users\pritesh\gogo>

No errors. I am good to go.

Executed it.

C:\Users\pritesh\gogo>go4.exe
My name is Pritesh Ugrankar, and my power is, 234.735000
C:\Users\pritesh\gogo>

My question is why there was no error shown when I compiled it earlier with %d?

As stated before, I am not trying to point out mistakes or something, just trying to understand it.
Regards,
pritesh

From the fmt package documentation:

If an invalid argument is given for a verb, such as providing a string to %d, the generated string will contain a description of the problem (…)

As far as the compiler is concerned, Printf is just some function from some library that takes a string and one or more parameters of unknown type:

func Printf(format string, a ...interface{}) (n int, err error)

Any type satisfies the empty interface, so the compiler has no reason to complain.


[EDIT] Fixed the link

1 Like

Hi christophberger,

Thank you for taking time to reply. My bad, I should have Read The Fine Manual. I am a Storage guy who gets very limited time to study Go and hence I simply went out and asked the question. I should have read the manual first. Thank you once again for explaining the reason.

Regards,
pritesh

Absolutely no problem, feel free to ask anything. The docs are often quite lengthy, making the desired info hard to find. And the second part of my answer isn’t even in the fmt document, so RTFM would not have helped.

1 Like

Hi christopherberger,

Thank you for the encouragement. I wish I would’ve gotten more time to devote to Go language, but whatever and whenever I can, I try.
Just bought Introducing Go Book. Loving it so far. :slight_smile:

Regards,
pritesh.

1 Like

vet can detect such correctness errors. Running vet on your input would emit:

arg power for printf verb %d of wrong type: float64
2 Likes

Hi Mohit,

Thanks for taking time to reply.

Regards,
pritesh

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.