A Tour of Go If with a short statement

Like for, the if statement can start with a short statement to execute before the condition.

Variables declared by the statement are only in scope until the end of the if.

(Try using v in the last return statement.)

package main  

import (
    "fmt"
    "math"
)

func pow(x, n , lim float64) float64 {
    if v := math.Pow(x,n); v < lim {
        return v
    }
    return lim
}

func main() {
    fmt.Println(
            pow(3, 2, 10),
            pow(3, 3, 20),
        )
}
时间: 2024-05-19 19:50:12

A Tour of Go If with a short statement的相关文章

A Tour of Go If and else

Variables declared inside an if short statement are also available inside any of the else blocks. package main import ( "fmt" "math" ) func pow(x, n, lim float64) float64{ if v := math.Pow(x, n); v < lim { return v } else { fmt.Prin

Go Flow Control

[Go Flow Control] 1.for没有(),必须有{}. 2.for的前后表达式可以为空. 3.没有while,for即是while. 4.无穷循环. 5.if没有(),必须有{}. 6.if临时变量. Like for, the if statement can start with a short statement to execute before the condition. Variables declared by the statement are only in s

Loop List

Loop List is very common in interview. This article we give a more strict short statement about its solving. One method to check if there's a loop in a list is to use two speed pointers. But most of articles did not give a proof. Assume we have a lis

[转]Clean Code Principles: Be a Better Programmer

原文:https://www.webcodegeeks.com/web-development/clean-code-principles-better-programmer/ ----------------------------------------------------------------- "My code is working well, the website I built is looking great, and my client is happy. So why

golang基础练习(一)

//遍历map package main import "fmt" func main() { x := make(map[string]int) x["zhangsan"] = 3 x["lisi"] = 4 x["wangwu"] = 5 //#丢弃值 for i,_ := range x { fmt.Println(i) } } //匿名函数 package main import "fmt" fun

A Tour of Go Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type. Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available. package main im

POJ 2135.Farm Tour 最小费用流

Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17307   Accepted: 6687 Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of

POJ 2135 Farm Tour

Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 213564-bit integer IO format: %lld      Java class name: Main When FJ's friends visit him on the farm, he likes to show them around. His farm comprise

Farm Tour

题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 1000