Baham's Blog

Above all else, guard your heart.

Go语言之旅22

A Tour of Go 22

package main

import (
    "fmt"
        "math"
        )

        func pow(x, n, lim float64) float64 {
            if v := math.Pow(x, n); v < lim {
             return 
            } else {
                 fmt.Printf( "%g >= %g\n", v, lim)
            }
                                // can't use v here, though
            return lim
        }

        func main() {
             fmt.Println(
                 pow(3, 2, 10),
                 pow(3, 3, 20),
            )
                                                                    }

开始时,这段代码一直没有看懂,不过,在Google、查找文档后,最终搞明白了。

首先,想说这段代码不复杂,由于只是初学者,所以会犯一些低级错误。

输出为:

27 >= 20
9 20

尤其是这一句:

fmt.Printf( "%g >= %g\n", v, lim)

后来在


 Floating-point and complex constituents:

    %b  decimalless scientific notation with exponent a power of two,
        in the manner of strconv.FormatFloat with the 'b' format,
        e.g. -123456p-78
    %e  scientific notation, e.g. -1234.456e+78
    %E  scientific notation, e.g. -1234.456E+78
    %f  decimal point but no exponent, e.g. 123.456
    %g  whichever of %e or %f produces more compact output
    %G  whichever of %E or %f produces more compact output

在godoc的fmt页(http://127.0.0.1:6060/pkg/fmt/) 有专门对Printf的介绍,

%g用于代表后边的输出量。




The Original Link: http://baham.github.io/11_16_goyu-yan-zhi-lu-22.html
If you want to reprint it, please do under the CC BY-NC-SA 4.0

Comments