限制存储错误的变量的范围

huangapple go评论83阅读模式
英文:

Limit the scope of variables storing error

问题

我有以下代码:

if entryJson, err := json.MarshalIndent(entry, "", " "); err != nil {
    log.Println(err)
} else {
    log.Println(entryJson)
}

if err := ioutil.WriteFile("text.json", entryJson, 0644); err != nil {
    log.Println(err)
}

我想尽可能地限制_err_的作用范围。我在这方面遇到的问题是,当我想将变量entryJson写入文件时,它已经超出了作用域。
处理这个问题的惯用方式是什么?我应该只是重用变量_err_并在额外的_if_块中检查它吗?像这样:

entryJson, err := json.MarshalIndent(entry, "", " ")
if err != nil {
    log.Println(err)
} else {
    log.Println(entryJson)
}

err = ioutil.WriteFile("text.json", entryJson, 0644)
if err != nil {
    log.Println(err)
}

这样可以工作,但对我来说看起来不太易读。

英文:

I have the following code:

if entryJson, err := json.MarshalIndent(entry, "", " "); err != nil{
	log.Println(err)
} else {
	log.Println(entryJson)
}

if err := ioutil.WriteFile("text.json", entryJson, 0644); err != nil {
	log.Println(err)
}

I want to limit the scope of err as much as possible. The problem I'm facing with this is, that the variable entryJson is out of scope when I want to write it to the file.
What is the idiomatic way to deal with this. Should I just reuse the variable err and check for it in an additional if block? Like this:

entryJson, err := json.MarshalIndent(entry, "", " ")
if err != nil{
	log.Println(err)
} else {
	log.Println(entryJson)
}

err = ioutil.WriteFile("text.json", entryJson, 0644)
if err != nil{
	log.Println(err)
}

This works but looks less readable to me.

答案1

得分: 2

首先,没有必要将变量隔离开来。
其次,你可以在if语句中进行简写赋值,例如:

entryJson, err := json.MarshalIndent(entry, "", " ")
if err != nil{
    log.Println(err)
} else {
    log.Println(entryJson)
}

if err = ioutil.WriteFile("text.json", entryJson, 0644); err != nil{
    log.Println(err)
}

// 或者如果你想要严格限制err的作用域,也可以这样写:

if err := ioutil.WriteFile("text.json", entryJson, 0644); err != nil{
    log.Println(err)
}

处理这个特定示例的一种简洁方法是将其放入一个单独的函数中并调用它:

func writeJSON(fn string, v interface{}) error {
    j, err := json.MarshalIndent(v, "", " ")
    if err != nil {
        return err
    }

    return ioutil.WriteFile(fn, j, 0644)
}

func main() {
    var test struct {
        A string
        B string
    }
    if err := writeJSON("file.json", test); err != nil {
        log.Fatal(err)
    }
}
英文:

First, there's no need to isolate variables.
Second, you can do short-hand assignment inside if statements, for example:

entryJson, err := json.MarshalIndent(entry, "", " ")
if err != nil{
    log.Println(err)
} else {
    log.Println(entryJson)
}

if err = ioutil.WriteFile("text.json", entryJson, 0644); err != nil{
    log.Println(err)
}

// or if you want to limit the scope of err badly, this is also legal:

if err := ioutil.WriteFile("text.json", entryJson, 0644); err != nil{
    log.Println(err)
}

A clean way to handle this specific example though is to put it in it is own function and call it:

func writeJSON(fn string, v interface{}) error {
	j, err := json.MarshalIndent(v, "", " ")
	if err != nil {
		return err
	}

	return ioutil.WriteFile(fn, j, 0644)
}

func main() {
	var test struct {
		A string
		B string
	}
	if err := writeJSON("file.json", test); err != nil {
		log.Fatal(err)
	}
}

答案2

得分: 0

你可以在错误检查后的else语句中编写文件,尽管我不能说这是惯用的/可读的。

var entry = []byte(`{
	"name": "bob",
	"age" : 74
}`)

func main() {
	if entryJson, err := json.MarshalIndent(entry, "", " "); err != nil {
		log.Fatal(err)
	} else {
		if err = ioutil.WriteFile("text.json", entryJson, 0644); err != nil {
			log.Fatal(err)
		} 	
	}
}
英文:

You can write file in the else statement following the error checking, although I can't say it's idiomatic/readable.

var entry = []byte(`{
	"name": "bob",
	"age" : 74
}`)

func main() {
	if entryJson, err := json.MarshalIndent(entry, "", " "); err != nil {
		log.Fatal(err)
	} else {
		if err = ioutil.WriteFile("text.json", entryJson, 0644); err != nil {
			log.Fatal(err)
		} 	
	}
}

huangapple
  • 本文由 发表于 2016年4月10日 22:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/36531188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定