C
C
cybervito212014-11-08 22:05:34
Google
cybervito21, 2014-11-08 22:05:34

How to get rid of error handling curve in GO?

Essence - we open the file, there is json, we decode it.
A few lines on go:

file, err := os.Open("config.json")
ifError(err)
decoder := json.NewDecoder(file)
config := Config{}
err := decoder.Decode(&config)
ifError(err)

Question - how to make more elegant error handling on GO

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey K, 2014-11-09
@cybervito21

There is no other way to handle errors in Go. You just need to write the code correctly.

config := Config{}
data, _ := ioutil.ReadFile("config.json")
err := json.Unmarshal(data, &config)

A
Alex, 2015-01-22
@alehano

Error handling is not crooked, but very good. If you don't want to handle errors, assign them to the void _ :=
Synthetic sugar of assignment and comparison in one line
err := json.Unmarshal(data, &config); if err != nil {
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question