Implementation details of fmt.Println in golang

后端 未结 1 327
一向
一向 2021-01-21 04:50

Consider this code

import (
  \"fmt\"
  \"math/big\"
)

func main() {
    var b1,b2,b3,bigSum big.Float

    b1.SetFloat64(25.3)
    b2.SetFloat64(76.2)
    b1.S         


        
1条回答
  •  走了就别回头了
    2021-01-21 04:55

    1. Println determines whether the value implements the Stringer interface. If it does then it will call the String() to get formatted value. big.Float implements it for pointer receiver so you have to pass a reference. Otherwise Println will detect that it's a struct and print all of it's fields using reflection
    2. Go is open sourced. You can see for yourself https://golang.org/src/fmt/print.go?#L738 It uses type switches and reflection.

    0 讨论(0)
提交回复
热议问题