[Solved] Why use the + sign in printfl?


fmt.Println is a variadic function whose arguments are generic interfaces. Any type can fulfill this interfere, including strings and floats. The second example works for this reason.

The first example, however, involves the binary operator +. As https://golang.org/ref/spec#Operators says, binary operators work in identical types. This means you can’t “add” a float to a string without first explicitly casting to a string.

In general, this is a decision the golang inventors made. If you read the design tenets of go, I think you’ll find this aligns well. But for the purposes of your question, it’s sufficient to say, that’s how it was made to work.

4

solved Why use the + sign in printfl?