Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding gometalinter's warning: conn can be io.Reader/io.Writer

Tags:

io

go

After checking with gometalinter this part of code:

//ListenerButton is hanging listeners for contact button
func ListenerButton(number int, button *ui.Button, conn net.Conn) string {
    button.OnClicked(func(*ui.Button) {
        sliceMembers := []string{login, button.Text()}
        groupName = login + button.Text()
        _, err := conn.Write([]byte(JSONencode(login, "", "",
            0, groupName, 1,
            login, sliceMembers, " ", " ", "",
            " ", " ", " ", true, " ", "CreateGroup")))
        if err != nil {
            log.Println(err)
        }
        fmt.Println(login, groupName, number, "graphic 131")
    })
    return groupName
}

I've got this warning:

warning: conn can be io.Writer (interfacer)

What does it mean and how i have to resolve it?

like image 660
Mykhailo Tkachenko Avatar asked Jun 17 '18 11:06

Mykhailo Tkachenko


People also ask

What is Io writer in Golang?

CODE EXAMPLE An io.Writer is an entity to which you can write a stream of bytes. The standard library contains many Writers, and Writers are accepted as input by many utilities. About Home Algorithms Go How to use the io.Writer interface yourbasic.org/golang Basics How to use a built-in writer (3 examples) Optimize string writes Basics

What is IO Reader in C++?

CODE EXAMPLE An io.Reader is an entity from which you can read a stream of bytes. The standard library has many Reader implementations, including in-memory byte buffers, files and network connections. Readers are accepted as input by many utilities such as HTTP clients and server implementations.

What is an IO writer?

CODE EXAMPLE An io.Writer is an entity to which you can write a stream of bytes. The standard library contains many Writers, and Writers are accepted as input by many utilities. About Home Algorithms Go How to use the io.Writer interface


1 Answers

That means that the ListenerButton function is only only using the Write method of conn. By changing the type from net.Conn to io.Writer, that allows your function to use a much larger number of io.Writer implementations. Having as small of interfaces as possible should be a goal when implementing your API.

With that change, for example, you could use an io.MultiWriter to write debug information to stderr, as well as the network connection:

func ListenerButton(number int, button *ui.Button, conn io.Writer) string {
    // ...
}

ListenerButton(number, button, io.MultiWriter(os.Stderr, networkConn))
like image 56
Tim Cooper Avatar answered Sep 25 '22 19:09

Tim Cooper