golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.68k stars 17.49k forks source link

cmd/go2go: 2-valued values cannot be assigned to two variables. #40815

Closed YoshikiShibata closed 4 years ago

YoshikiShibata commented 4 years ago

2-valued values cannot be assigned to two variables: https://go2goplay.golang.org/p/rKe_LDnHgM_m

package main

type Predicate[type T] func(t T) bool

type prevStream[type T] struct {
    req  chan struct{}
    data chan T
}

func (ps prevStream[T]) prevData() (T, bool) {
    ps.req <- struct{}{}
    d, ok := <-ps.data
    return d, ok
}

func (ps prevStream[T]) closePrev() {
    close(ps.req)
}

type genericStream[type T] struct {
    prevStream[T]
    nextReq  chan struct{}
    nextData chan T
}

func (gs *genericStream[T]) filter(predicate Predicate[T]) {
    for range gs.nextReq {
        data, ok := <-gs.prevStream.prevData()
        if !ok {
            close(gs.nextData)
            gs.prevStream.closePrev()
            return
        }

        for !predicate(data) {
            data, ok = <-gs. prevStream.prevData()
            if !ok {
                close(gs.nextData)
                gs.prevStream.closePrev()
                return
            }
        }
        gs.nextData <- data
    }
}

func main() {
}
type checking failed for main
prog.go2:28:17: 2-valued gs.prevStream.prevData() (value of type (T, bool)) where single value is expected
prog.go2:36:17: 2-valued gs.prevStream.prevData() (value of type (T, bool)) where single value is expected
zephyrtronium commented 4 years ago

You're using the receive operator on the result of gs.prevStream.prevData(), which is two values, not a channel. The receive operator cannot be used on two values, hence the type check failure.

ianlancetaylor commented 4 years ago

Agreed. This is not a bug.

YoshikiShibata commented 4 years ago

Oops. Sorry for bothering.