blogng/blog/2016-01-14-golang-shorthand-operator-allows-accidental-shadowing-of-variable.markdown
Dhananjay Balan 58b20109cf Convert from old categories to tags
import sys
import yaml

with open(sys.argv[1]) as fp:
    data = fp.read()

if not data.find("---") == 0:
    # no head
    print("NO YAML HEAD FOUND")
    sys.exit(-1)

data = data[3:]
head_end = data.find("---")

head = data[0:head_end]
data = data[head_end+3:]

metadata = yaml.safe_load(head)

cats = metadata.pop('categories', None)
if cats != None:
    if type(cats) == list:
        tags = cats
    elif type(cats) == str:
        tags = cats.split()

    tags = list(map(lambda t: t.lower(), tags))

    metadata["tags"] = ", ".join(tags)
    new_data = f"---\n{yaml.dump(metadata, default_flow_style=False)}---{data}"
    # write it
    print(f"coverted: categories to tags: {tags} - {sys.argv[1]}")
    with open(sys.argv[1], "w") as fp:
        fp.write(new_data)
    sys.exit(0)

if not metadata.get("tags", None):
    metadata["tags"] = "untagged"
    new_data = f"---\n{yaml.dump(metadata, default_flow_style=False)}---{data}"
    print(f"untagged: {sys.argv[1]}")
    # write it
    with open(sys.argv[1], "w") as fp:
        fp.write(new_data)
    sys.exit(0)

print("No changes needed")
2019-01-28 17:16:27 -05:00

132 lines
2.2 KiB
Markdown

---
comments: true
date: 2016-01-14 21:13
layout: post
tags: go
title: 'go: `:=` operator causes accidental shadowing'
---
Go provides `:=` operator to make declaring variables easier. It is a [shorthand to declare and set a value of a variable](https://golang.org/ref/spec#Short_variable_declarations). for example,
```go
var x int
x = 42
```
can be written as
```go
x := 42
```
But if not careful, this can accidently shadow variable bindings. Let's look at the fictitious piece of code.
```go
package main
import "fmt"
func fictitiousFunc() (int, error) {
return 42, nil
}
func main() {
x := 10;
x, err := fictitiousFunc()
if err != nil {
fmt.Println("I'll never print")
}
fmt.Println("value of x: ", x)
}
```
This produces following output
```
value of x: 42
```
While, this following piece of code will fail to compile
```go
package main
import "fmt"
func fictitiousFunc() (int, error) {
return 42, nil
}
func main() {
x := 10
// replace :=
var x int
var err error
x, err = fictitiousFunc()
if err != nil {
fmt.Println("I'll never print")
}
fmt.Println("value of x: ", x)
}
```
output:
```
prog.go:12: x redeclared in this block
previous declaration at prog.go:10
```
So we can see that the operator is somewhat intelligent, and does not redeclare the variables.
Now what if we push it down a scope? See the following code
```go
package main
import "fmt"
func fictitiousFunc() (int, error) {
return 42, nil
}
func main() {
someCondition := true
x := -1;
if someCondition {
x, err := fictitiousFunc()
if err != nil {
fmt.Println("I'll never print")
}
fmt.Println("value of x inside: ", x)
}
fmt.Println("value x outside: ", x)
}
```
This produces,
```go
value of x inside: 42
value x outside: -1
```
At line: 16, since the immediate scope (line:15-32) does not have variable `x` declared, `:=` is redeclaring the variable. a.k.a the __variable `x` gets shadowed__.
Only workaround I can think of is not to use `:=`, i.e change the code to
```go
if someCondition {
var err error
x, err = fictitiousFunc()
if err != nil {
fmt.Println("I'll never print")
}
fmt.Println("value of x inside: ", x)
}
```
If you know something better let me know.