Golang type switch requires (redundant) type assertion - go

I would like to use a type switch to call a type specific parsing function
https://play.golang.org/p/2xj_owLL4ZK
package main
import (
"fmt"
)
func main() {
var value interface{}
value = "I am a string"
switch v := value.(type) {
case string:
parseString(value)
default:
fmt.Printf("I don't know about type %T!\n", v)
}
}
func parseString(s string) {
fmt.Println(s)
}
However this does not compile because it's missing a type assertion:
cannot use value (type interface {}) as type string in argument to parseString: need type assertion
Adding a type assertion fixes the error.
https://play.golang.org/p/p0nYNEEJb0Z
package main
import (
"fmt"
)
func main() {
var value interface{}
value = "I am a string"
switch v := value.(type) {
case string:
s, ok := value.(string)
if ok {
parseString(s)
}
default:
fmt.Printf("I don't know about type %T!\n", v)
}
}
func parseString(s string) {
fmt.Println(s)
}
But this feels redundant. I am now checking twice, whether the value is a string.
Should I choose between a type switch and type assertion? Perhaps there is a less redundant way to do this? The example is contrived. There could be many types, which is why a type switch seemed like clean solution...until I started adding type asseertions.
Update
This question has received multiple downvotes. I think this misses the confusing nature of Go's type switch where it appears (initially) as if the value being switched on is the type, not the value.
switch v := value.(type) {
case string:
// ...
case int:
// ...
}
I'm new to Go and incorrectly assumed v was the type. If I ran into this problem when writing Go for this first time, others may too?

Use the value you declared in the switch:
switch v := value.(type) {
case string:
// v is string here
parseString(v)
...

Related

Why is a separate variable declared in a Go type switch?

I am having trouble understanding why type switches are written with an additional variable defined in the switch statement. The below code seems to be the sanctioned way of doing things:
func test_func(i interface{}) {
switch v := i.(type) {
case int:
fmt.Printf("%T\n", v)
case float64:
fmt.Printf("%T\n", v)
case int:
fmt.Printf("I don't know about type %T!\n", v)
}
}
func main() {
test_func(float64(34))
test_func(int(34))
test_func("hello world")
}
As expected, this returns:
float64
int
I don't know about type string!
However, I can change test_func slightly so that v is not defined in the switch statement, and instead we use i inside our case statements:
func test_func(i interface{}) {
switch i.(type) {
case int:
fmt.Printf("%T\n", i)
case float64:
fmt.Printf("%T\n", i)
case int:
fmt.Printf("I don't know about type %T!\n", i)
}
}
func main() {
test_func(float64(34))
test_func(int(34))
test_func("hello world")
}
And the output is not changed. It seems the two forms are interchangeable. Why would I go to the trouble of defining v when I could just use i? The latter case is simpler since there is one less variable to keep track of; maybe it is even more performant.
They are not interchangeable; you're just passing i to a function that can take it regardless of its type (fmt.Printf's arguments after the format string are of type interface{}). i is still its original type, because the type of a variable cannot change.
If you actually wanted to do something with it based on its type, you would need the first form, so that v would be of the type in the case statement. Whether or not you assign the typed value to a variable, the original variable i retains its original type.
This is covered well in the Tour of Go: Type switches

How do you make a function accept multiple types?

I am having a function like this:
package main
import "flag"
import "fmt"
func print_out_type(x anything) string {
switch v := x.(type) {
case string:
return "A string"
case int32:
return "An Integer"
default:
return "A default"
}
}
func main() {
wordPtr := flag.String("argument1", "foo", "a String")
numPtr := flag.Int("argument2", 42, "an Integer")
flag.Parse()
fmt.Println("word: ", *wordPtr)
fmt.Println("number: ", *numPtr)
}
I am trying to return different types of strings based on the type. I am just stuck at the point of how do I write a function that accepts arguments of different types.
You can use interface types as arguments, in which case you can call the function with any type that implements the given interface. In Go types automatically implement any interfaces if they have the interface's methods. So if you want to accept all possible types, you can use empty interface (interface{}) since all types implement that. No other modification needs to be done to your function.
func print_out_type(x interface{}) string {
switch v := x.(type) {
case string:
return "A string"
case int32:
return "An Integer"
default:
return "A default"
}
}
You can also use the reflect package to study the type of an interface variable. For Example:
func print_out_type(x interface{}) string {
return reflect.TypeOf(x).String()
}
func main() {
fmt.Println(print_out_type(42))
fmt.Println(print_out_type("foo"))
}
Will print
int
string
Starting with Go 1.18. you can use generics to specify which types can be used in a function.
func print_out_type[T any](x T) string {
return fmt.Sprintf("%T", x)
}
https://go.dev/doc/tutorial/generics
First, if you are using a type assertion, you need to check that this operation is successful. eg: value, ok := temp.(int)
Second, you can use the interface{} type variable as a parameter to the function. In the Function, you can use type assertion to check the parameter's real type and print out.

Perform an action on a collection of items

In my actual code, I'm parsing an XML document using encoding/xml, and I basically have a bunch of nested structures of the following form — all of which may occur multiple times, except the top-level statements element:
statements
statement
opcode
args
pre
post
I'm fairly new to Go, and I'm clearly misunderstanding how interface{} (the empty interface) works:
.\stmtgen.go:58: cannot use print_name (type func(Statement)) as type func(interface {}) in argument to performAction
.\stmtgen.go:58: cannot use slist (type []Statement) as type []interface {} in argument to performAction
Relevant example code:
package main
import "fmt"
// Actually a structure in my code, but this suffices for demonstration.
type Opcode int
// A Statement has a Name and multiple Opcodes may use this Name.
type Statement struct {
Name string
Opcodes []Opcode
}
// Print the statement name.
func print_name(stmt Statement) {
fmt.Println(stmt.Name)
}
// Perform an action on each item of a collection.
func performAction(action func(interface{}), v []interface{}) {
for i := range v {
action(v[i])
}
}
func main() {
slist := make([]Statement, 3)
slist[0] = Statement{"Statement 1"}
slist[1] = Statement{"Statement 2"}
slist[2] = Statement{"Statement 3"}
//ERROR HERE
performAction(print_name, slist)
}
Must I create functions to print the values for every single type?
An empty interface{} can contain any value and passed around as the type interface{}. when you need the value from it, you can perform a type assertion like this:
var anyValue interface{}
anyValue = "hello"
strValue := anyValue.(string)
If anyValue is not of the type being asserted then it will cause a panic
the type assertion can also be used to return a bool if the interface is of that type with a multiple return
strValue, ok := anyValue.(string)
if ok {
//anyValue contains a string!
}
If you dont know the type of the interface, you can use a switch to determine it's type like this:
switch val := anyValue.(type) {
case string:
// anyValue contains a string
// and val is a string
break
case int:
// anyValue contains an int
// and val is and int
break
default:
//unhandled interface type
}
Hopefully this makes the empty interface{} type clearer.
interfaces{...} which have methods declared in them are different, they can not have members (like structs can), only methods, and their underlying type must implement all the methods declared in the interface. You could have an interface actionPerformer (interface names should have the suffix "er" as they are doing something)
type actionPerformer interface {
action(interface{})
}
A type that implements all the methods in an interface can be cast to that interface type, then if you call one of those methods on the interface, it will run the method on the underlying type.
For example, if the Statement struct implements the action(interface{}) method, the Statement struct can be cast to an actionPerformer type and if the action(interface{}) function is called on the actionPerformer, the action function on the Statement struct is run. So you could have multiple types that all have the action(interface{}) method and they can all be cast to an actionPerformer which you can call the action function on.
func (code Opcode) action(arg interface{}) {
fmt.Println(arg.(int) + int(code))
}
func (stmt Statement) action(arg interface{}) {
fmt.Println(arg.(string), stmt.Name)
}
stmt := Statement{"Statement 1", nil}
stmtActionPerformer := actionPerformer(stmt)
opcode := Opcode(5)
opcodeActionPerformer := actionPerformer(opcode)
stmtActionPerformer.action("hello") // will print "hello "+whatever the statements name is
opcodeActionPerformer.action(2) //will print be 7
Type assertions can still be used on these types of interface e.g.
stmt := stmtActionPerformer.(Statement)
fmt.Println(stmt.Name)
This is a contrived example, but with this in mind, you might want to write your code using interfaces like this.
Remember casting between interfaces is costly, so should be done sparingly, however they are a powerful tool when used correctly.
For your example, a simple printNames function would be much more efficient than all that interface casting (note that in golang, names should be in the CamelCase format, not using underscores)
func printNames(stmts []Statement) {
for _, stmt := range stmts {
fmt.Println(stmt.Name)
}
}
It might also be useful to have a type StatementList and add methods to it:
type StatementList []Statement
func (list StatementList) printNames() {
for _, stmt := range list {
fmt.Println(stmt.Name)
}
}
Getting the hang of this stuff make golang a lot more fun, hope this helps :)
You have to declare the parameters of performAction exactly same like the arguments type.
func performAction(action func(Statement), v []Statement) {
for i := range v {
action(v[i])
}
}
Or you could use interface{} on all parameters instead. Then cast it according to the needs.
func performAction(action interface{}, v interface{}) {
for _, each := range v.([]Statement) {
action.(func(Statement))(each)
}
}
data with type []Statement cannot be assigned to []interface{}
also for type func(Statement) cannot be assigned to func(interface{})
Use interface{}, then cast it to the original type.
this works for me:
package main
import (
"fmt"
)
// Actually a structure in my code, but this suffices for demonstration.
type Opcode int
// A Statement has a Name and multiple Opcodes may use this Name.
type Statement struct {
Name string
Opcodes []Opcode
}
// Print the statement name.
func print_name(stmt interface{}) {
if s, ok := stmt.(Statement); !ok {
panic("typ err")
} else {
fmt.Println(s.Name)
}
}
// Perform an action on each item of a collection.
func performAction(action func(interface{}), v []interface{}) {
for i := range v {
action(v[i])
}
}
func main() {
slist := make([]interface{}, 3)
slist[0] = Statement{"Statement 1", nil}
slist[1] = Statement{"Statement 2", nil}
slist[2] = Statement{"Statement 3", nil}
performAction(print_name, slist)
/*output:
Statement 1
Statement 2
Statement 3
*/
}

How to convert interface{} to bytes.Buffer

How to convert interface{} to bytes.Buffer?
Minimal example
package main
import (
"bytes"
"fmt"
)
func ToJson5(any interface{}) string {
if any == nil {
return `''`
}
switch any.(type) {
case bytes.Buffer: // return as is
return any.(bytes.Buffer).String()
// other types works fine
}
return ``
}
func main() {
x := bytes.Buffer{}
fmt.Println(ToJson5(x))
}
The errors was:
main.go:14: cannot call pointer method on any.(bytes.Buffer)
main.go:14: cannot take the address of any.(bytes.Buffer)
When changed to bytes.Buffer{} (that I think less correct), the error was:
main.go:13: bytes.Buffer literal (type bytes.Buffer) is not a type
main.go:14: bytes.Buffer literal is not a type
You can use a Short variable declaration in the Type switch to have the typed value in the case branches:
switch v := any.(type) {
case bytes.Buffer: // return as is
return v.String() // Here v is of type bytes.Buffer
}
Try it on the Go Playground.
Quoting form the spec:
The TypeSwitchGuard may include a short variable declaration. When that form is used, the variable is declared at the beginning of the implicit block in each clause. In clauses with a case listing exactly one type, the variable has that type; otherwise, the variable has the type of the expression in the TypeSwitchGuard.

cannot convert data (type interface {}) to type string: need type assertion

I am pretty new to go and I was playing with this notify package.
At first I had code that looked like this:
func doit(w http.ResponseWriter, r *http.Request) {
notify.Post("my_event", "Hello World!")
fmt.Fprint(w, "+OK")
}
I wanted to append newline to Hello World! but not in the function doit above, because that would be pretty trivial, but in the handler afterwards like this below:
func handler(w http.ResponseWriter, r *http.Request) {
myEventChan := make(chan interface{})
notify.Start("my_event", myEventChan)
data := <-myEventChan
fmt.Fprint(w, data + "\n")
}
After go run:
$ go run lp.go
# command-line-arguments
./lp.go:15: invalid operation: data + "\n" (mismatched types interface {} and string)
After a little bit of Googling I found this question on SO.
Then I updated my code to:
func handler(w http.ResponseWriter, r *http.Request) {
myEventChan := make(chan interface{})
notify.Start("my_event", myEventChan)
data := <-myEventChan
s:= data.(string) + "\n"
fmt.Fprint(w, s)
}
Is this what I was supposed to do? My compiler errors are gone so I guess that's pretty good? Is this efficient? Should you do it differently?
According to the Go specification:
For an expression x of interface type and a type T, the primary expression x.(T) asserts that x is not nil and that the value stored in x is of type T.
A "type assertion" allows you to declare an interface value contains a certain concrete type or that its concrete type satisfies another interface.
In your example, you were asserting data (type interface{}) has the concrete type string. If you are wrong, the program will panic at runtime. You do not need to worry about efficiency, checking just requires comparing two pointer values.
If you were unsure if it was a string or not, you could test using the two return syntax.
str, ok := data.(string)
If data is not a string, ok will be false. It is then common to wrap such a statement into an if statement like so:
if str, ok := data.(string); ok {
/* act on str */
} else {
/* not string */
}
Type Assertion
This is known as type assertion in golang, and it is a common practice.
Here is the explanation from a tour of go:
A type assertion provides access to an interface value's underlying concrete value.
t := i.(T)
This statement asserts that the interface value i holds the concrete type T and assigns the underlying T value to the variable t.
If i does not hold a T, the statement will trigger a panic.
To test whether an interface value holds a specific type, a type assertion can return two values: the underlying value and a boolean value that reports whether the assertion succeeded.
t, ok := i.(T)
If i holds a T, then t will be the underlying value and ok will be true.
If not, ok will be false and t will be the zero value of type T, and no panic occurs.
NOTE: value i should be interface type.
Pitfalls
Even if i is an interface type, []i is not interface type. As a result, in order to convert []i to its value type, we have to do it individually:
// var items []i
for _, item := range items {
value, ok := item.(T)
dosomethingWith(value)
}
Performance
As for performance, it can be slower than direct access to the actual value as show in this stackoverflow answer.
//an easy way:
str := fmt.Sprint(data)
As asked for by #ρяσѕρєя an explanation can be found at https://golang.org/pkg/fmt/#Sprint. Related explanations can be found at https://stackoverflow.com/a/44027953/12817546 and at https://stackoverflow.com/a/42302709/12817546. Here is #Yuanbo's answer in full.
package main
import "fmt"
func main() {
var data interface{} = 2
str := fmt.Sprint(data)
fmt.Println(str)
}
In addition to other answers, I think it's good to have a look at "type switch":
package main
import "fmt"
func printType(i interface{}) {
switch v := i.(type) {
case int:
fmt.Printf("type of %v is %v\n", i, v)
// type of 21 is int
case string:
fmt.Printf("type of %v is %v\n", i, v)
// type of hello is string
default:
fmt.Printf("type of %v is %v\n", i, v)
// type of true is bool
}
}
func main() {
printType(21)
printType("hello")
printType(true)
}
I hope it helps.
More information: https://go.dev/tour/methods/16

Resources