Answer the question
In order to leave comments, you need to log in
How in GOlang to specify in a function that the return type will be either an array or something else?
Good afternoon
For example, there is a function:
func getMsgPack(row string) []byte{
in := strings.Split(row,"\t")
if msg_format == "json" {
b, err := json.Marshal(in)
}else{
b, err := msgpack.Marshal(in)
} // if
failOnError(err, strings.Join([]string{"Failed to convert msg to msgPack. Format: ", msg_format},""))
return b
} // func
failOnError(err, strings.Join([]string{"Failed to convert msg to msgPack. Format: ", msg_format},""))
return b
Answer the question
In order to leave comments, you need to log in
Answer to this question:
Go is a statically typed language.
You need to either explicitly convert.
Or use interface{} - but it is not recommended to abuse it.
The answer is based on the source code you attached and the text of the error: But you have a different
problem .
You declared variables "b" and "err" inside blocks
if {
} else {
}
An error is thrown:
undefined b
undefined err
on these 2 lines:
var b []byte
var err error
if msg_format == "json" {
b, err = json.Marshal(in)
} else {
b, err = msgpack.Marshal(in)
}
func getMsgPack(row string) ([]byte, string) {
// omited
return b, "json"
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question