Home > Backend Development > Golang > How Can Go\'s Error Handling Be Improved for Readability and Conciseness?

How Can Go\'s Error Handling Be Improved for Readability and Conciseness?

DDD
Release: 2024-11-26 05:00:10
Original
382 people have browsed it

How Can Go's Error Handling Be Improved for Readability and Conciseness?

Error Handling in Go

Error handling in Go is a crucial aspect of programming. The language encourages direct error checking and handling, which can lead to verbose code handling errors repetitively.

Consider the following contrived example that pipes "Hello world!" into cat and reads the output:

package main

import "fmt"
import "io"
import "io/ioutil"
import "os/exec"


func main() {
    cmd := exec.Command("cat", "-")
    stdin, err := cmd.StdinPipe()
    if err != nil {
        return
    }
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        return
    }
    err = cmd.Start()
    if err != nil {
        return
    }
    _, err = io.WriteString(stdin, "Hello world!")
    if err != nil {
        return
    }
    err = stdin.Close();
    if err != nil {
        return
    }
    output, err := ioutil.ReadAll(stdout)
    if err != nil {
        return
    }
    fmt.Println(string(output))
    return
}
Copy after login

This example has multiple lines of error handling code for each operation.

An idiomatic approach in Go is to handle errors in a centralized way. For instance, we can encapsulate the piping operation in a custom function that manages errors internally:

package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "os/exec"
)

func piping(input string) (string, error) {
    cmd := exec.Command("cat", "-")
    stdin, err := cmd.StdinPipe()
    if err != nil {
        return "", err
    }
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        return "", err
    }
    err = cmd.Start()
    if err != nil {
        return "", err
    }
    _, err = io.WriteString(stdin, input)
    if err != nil {
        return "", err
    }
    err = stdin.Close()
    if err != nil {
        return "", err
    }
    all, err := ioutil.ReadAll(stdout)
    output := string(all)
    if err != nil {
        return output, err
    }
    return output, nil
}

func main() {
    in := "Hello world!"
    fmt.Println(in)
    out, err := piping(in)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(out)
}
Copy after login

This approach reduces the amount of error-handling code in the main function, making it more concise and easier to read.

The above is the detailed content of How Can Go\'s Error Handling Be Improved for Readability and Conciseness?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template