Git Product home page Git Product logo

Comments (2)

stv0g avatar stv0g commented on July 28, 2024

In Go, errors are handled using the built-in error interface, which is a simple and lightweight mechanism for representing errors. The error interface is defined as follows:

type error interface {
    Error() string
}

This interface has a single method Error() that returns a string representing the error message. Any type that implements this method automatically satisfies the error interface.

When a function in Go wants to indicate that an error has occurred, it returns an error value. If the function completes successfully, it returns nil. For example, consider a function that opens a file:

func OpenFile(filename string) (*File, error) {
    // Implementation to open the file
    // If successful, return the file and nil error
    // If an error occurs, return nil file and the corresponding error
}

To handle errors, you typically use the if statement to check if the returned error is nil or not. If it's nil, the operation was successful. Otherwise, you can handle the error appropriately. Here's an example:

func main() {
    file, err := OpenFile("example.txt")
    if err != nil {
        // Handle the error, e.g., log it, return an error, or exit the program
        fmt.Println("Error:", err)
        return
    }
    // Use the file, as it was successfully opened
    fmt.Println("File opened successfully.")
    file.Close()
}

Go encourages explicit error handling, which means you should not ignore errors returned from functions. Properly handling errors helps ensure that your programs behave predictably and reliably, even in unexpected situations.

In the example mentioned here, ErrInvalidMsgType is a custom error created by errors.New and as such implements the error interface mentioned above:

https://github.com/stv0g/go-rosenpass/blob/d7e38ecaf9e7803f2824a03ac24ac34944a53af6/messages.go#L13

from go-rosenpass.

stv0g avatar stv0g commented on July 28, 2024

Can we use unsigned values?
Where are unsigned values used in the application?
Unsigned values run the danger of causing subtraction where we expect addition if an attacker managed to inject them.

go-rosenpass is mainly relying signed integers.

The interface of UnmarshalBinary is mainly inspired by fmt.Fscanf() or io.Reader() which all use signed int's to return the number of octets parsed.

from go-rosenpass.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.