duanran3115 2017-04-14 13:04
浏览 9
已采纳

Golang的早期或晚期争论评估?

In my program I do a series of sequential checks in this manner:

var value int

if !(ParseOrFail(inputStrVal, &value) &&
     Validate(value)) {
    return SomeErr
}

I know that Validate is called only if ParseOrFail returns true, but I'm not sure whether in all such scenarios it will get the updated value.

Is it correct to do so? Or must I pass a pointer to Validate ?

Playground link: https://play.golang.org/p/l6XHbgQjFs

  • 写回答

1条回答 默认 最新

  • douke9379 2017-04-14 14:11
    关注

    The Go Programming Language Specification

    Expressions

    An expression specifies the computation of a value by applying operators and functions to operands.

    Operands

    Operands denote the elementary values in an expression. An operand may be a literal, a (possibly qualified) non-blank identifier denoting a constant, variable, or function, a method expression yielding a function, or a parenthesized expression.

    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.

    Calls

    Given an expression f of function type F,

    f(a1, a2, … an)
    

    calls f with arguments a1, a2, … an. Except for one special case, arguments must be single-valued expressions assignable to the parameter types of F and are evaluated before the function is called. The type of the expression is the result type of F. A method invocation is similar but the method itself is specified as a selector upon a value of the receiver type for the method.

    Logical operators

    Logical operators apply to boolean values and yield a result of the same type as the operands. The right operand is evaluated conditionally.

    &&    conditional AND    p && q  is  "if p then q else false"
    ||    conditional OR     p || q  is  "if p then true else q"
    !     NOT                !p      is  "not p"
    

    The behavior of your code is defined in The Go Programming Language Specification.

    var value int
    
    if !(ParseOrFail(inputStrVal, &value) && Validate(value)) {
        return SomeErr 
    }
    

    Or, in pseudocode,

    ParseOrFail arguments are evaluated
    ParseOrFail is called
    if ParseOrFail == true
        Validate arguments are evaluated
        Validate is called
    

    That is, in your example (https://play.golang.org/p/l6XHbgQjFs), late evaluation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么