Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lists of inconsistent types #9

Open
Elara6331 opened this issue Nov 16, 2021 · 0 comments
Open

Lists of inconsistent types #9

Elara6331 opened this issue Nov 16, 2021 · 0 comments

Comments

@Elara6331
Copy link

Elara6331 commented Nov 16, 2021

Describe the bug
When using a list of different types, it is implemented as []interface{} as it should, but getting a value does not assert it to the proper type

Python code example

def main():
	x = ["x", 1, 2, 3.0]
	x.append(True)
	print(x[1]+x[2])

if __name__ == '__main__':
  main()

Current behavior

package main

import "fmt"

func main() {
	x := []interface{}{"x", 1, 2, 3.0}
	x = append(x, true)
	fmt.Println(x[1] + x[2])
}

This creates an error because interfaces cannot be added.

Expected behavior
I expected that there would be a type assertion converting the values to int so they could be added

Go code example

package main

import "fmt"

func main() {
	x := []interface{}{"x", 1, 2, 3.0}
	x = append(x, true)
	fmt.Println(x[1].(int) + x[2].(int))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant