dongshedan4672 2016-08-18 05:13 采纳率: 0%
浏览 97
已采纳

为什么fmt.Println()的输出不是我所需要的

I am new to Golang, and I am confused by the output order of fmt.println(), here is my code

package main

import (
    "fmt"
    "math"
)

func pow(x, n, lim float64) float64 {
    if v := math.Pow(x, n); v < lim {
        return v
    } else {
        fmt.Printf("%g >= %g
", v, lim)
    }
    return lim
}

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

The output is

27 >= 20
9 20

And what I want is

9
27 >= 20
20

And add more print statement in pow():

func pow(x, n, lim float64) float64 {
    if v := math.Pow(x, n); v < lim {
        fmt.Println("___1___")
        return v
    } else {
        fmt.Printf("%g >= %g
", v, lim)
    }
    fmt.Println("___2___")
    return lim
}

the output is

___1___
27 >= 20
___2___
9 20

Why the output is that? Thanks!

  • 写回答

2条回答 默认 最新

  • dongzhuo1498 2016-08-18 06:07
    关注

    Order of evaluation:

    At package level, initialization dependencies determine the evaluation order of individual initialization expressions in variable declarations. Otherwise, when evaluating the operands of an expression, assignment, or return statement, all function calls, method calls, and communication operations are evaluated in lexical left-to-right order.

    For example, in the (function-local) assignment

    y[f()], ok = g(h(), i()+x[j()], <-c), k()
    

    the function calls and communication happen in the order f(), h(), i(), j(), <-c, g(), and k(). However, the order of those events compared to the evaluation and indexing of x and the evaluation of y is not specified.

    a := 1
    f := func() int { a++; return a }
    x := []int{a, f()}            // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified
    m := map[int]int{a: 1, a: 2}  // m may be {2: 1} or {2: 2}: evaluation order between the two map assignments is not specified
    n := map[int]int{a: f()}      // n may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified
    

    At package level, initialization dependencies override the left-to-right rule for individual initialization expressions, but not for operands within each expression:

    var a, b, c = f() + v(), g(), sqr(u()) + v()
    
    func f() int        { return c }
    func g() int        { return a }
    func sqr(x int) int { return x*x }
    
    // functions u and v are independent of all other variables and functions
    

    The function calls happen in the order u(), sqr(), v(), f(), v(), and g().

    Floating-point operations within a single expression are evaluated according to the associativity of the operators. Explicit parentheses affect the evaluation by overriding the default associativity. In the expression x + (y + z) the addition y + z is performed before adding x.


    If you need:

    9
    27 >= 20
    20
    

    First call pow(3, 2, 10) and Print the result, like this working sample code:

    package main
    
    import "fmt"
    import "math"
    
    func pow(x, n, lim float64) float64 {
        if v := math.Pow(x, n); v < lim {
            return v
        } else {
            fmt.Printf("%g >= %g
    ", v, lim)
        }
        return lim
    }
    
    func main() {
        fmt.Println(pow(3, 2, 10))
        fmt.Println()
    
        fmt.Println(pow(3, 3, 20))
    }
    

    output:

    9
    
    27 >= 20
    20
    

    See the order of function calls in this working sample code:

    package main
    
    import "fmt"
    
    func pow(n float64) float64 {
        fmt.Println("order:", n)
        return n
    }
    
    func main() {
        fmt.Println(pow(1), pow(2), pow(3))
    }
    

    output:

    order: 1
    order: 2
    order: 3
    1 2 3
    

    And:

    package main
    
    import "fmt"
    
    func pow(n int) int {
        fmt.Println("order:", n)
        return n
    }
    
    func main() {
        a, b, c := pow(1), pow(2), pow(3)
        fmt.Println("Order 4")
        fmt.Println(a, b, c)
    }
    

    output:

    order: 1
    order: 2
    order: 3
    Order 4
    1 2 3
    

    package main
    
    import "fmt"
    
    func pow(n int) int {
        fmt.Println("order:", n)
        return n
    }
    
    func main() {
        a := pow(1)
        b := pow(2)
        c := pow(3)
        fmt.Println("Order 4")
        fmt.Println(a, b, c)
    }
    

    output:

    order: 1
    order: 2
    order: 3
    Order 4
    1 2 3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口