2020/02/27

こういった場合に便利なのがオフィシャルが提供している解析コマンド shadow です。(相変わらずググらび...)

インストールは以下を実行します。

$ go get golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow

ツイートされておられる以下のコードで実行してみます。

package main

import (
    "fmt"
)

var condition = true

func main() {

    var hoge *string
    if condition {
        hoge, err := do("word")
        if err != nil {
            return
        }
        fmt.Printf("checkpoint: %v\n", *hoge)
    } else {
        hoge = nil
    }

    fmt.Printf("RESUT: %v\n", hoge)

}

func do(v string) (*stringerror) {
    return &v, nil
}
.../main.go:13:3: declaration of "hoge" shadows declaration at line 11

便利。

改訂2版 みんなのGo言語 改訂2版 みんなのGo言語
松木 雅幸, mattn, 藤原 俊一郎, 中島 大一, 上田 拓也, 牧 大輔, 鈴木 健太
技術評論社 Kindle版 / ¥2,350 (2019年08月01日)
 
発送可能時間:

Posted at 10:23 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.