I'm working towards implementing a concurrent string
and byte
reader in Go. The purpose of this is to allow parsing of newline
and other such bytes in the read strings.
In researching this problem I've found various ways of handling multiple values in a single value context 1, however none of these deal with the occurrence of mixed types. The idea of using an interface to deal with this has been suggested 2, and has been attempted, but I am uncomfortable with the lengthy verbosity of the exisitng suggestions 3 and 4.
I wonder if there is an idiomatic way to ectively sort through a variety of typed values in a tidy fashion.
EDITED: First I established an interface, as suggested. This seems a good idea, and is a commonly used trick from C if I recall.
func Use(vals ...interface{}) {
i := 0
p := []uint8{} //I've replaced the alias "byte" with the native "uint8"
var val uint8 //I've changed this declaration to a non-assigned declaration
for i, val := range vals {
if i < 1 {
_ = val
i++
} else {
p[i] = val.(uint8)
return fmt.Print(p[i]) //please excuse the earlier typo
//interestingly, this call to p[i] returns more than one value
}
}
}
The rest of the essential code follows:
func other() (string, []byte) {
a := "declared and not used"
b := []byte("stuff")
return a,b
}
func main() {
Use(other())
}
It remains unbeknownst to me why this code should appear to have multiple values inside p[i]. Shouldn't the blank identifier
used in the control loop make such a possibility unlikely?
The new error, from the edited code is reported as:
invalid type assertion: x.(uint8) (non-interface type func(...interface {}) []interface {} on left)
The original code can be found at: https://play.golang.org/p/BEhOT7R0vvr
The edited code can be found at: https://goplay.space/#SF7X7dx8yL9