type-assertion

Removing an element from a type asserted Slice of interfaces

六眼飞鱼酱① 提交于 2021-02-04 19:57:50
问题 In Golang, after asserting to a slice, how is one able to remove an element from said slice? For example, the following returns the error cannot assign to value.([]interface {}) value.([]interface{}) = append(value.([]interface{})[:i],value.([]interface{})[i+1:]...) 回答1: If you have a slice value wrapped in an interface, you can't change it. You can't change any value wrapped in interfaces. When an interface value is created to wrap a value, a copy is made and stored in the interface. When

Removing an element from a type asserted Slice of interfaces

主宰稳场 提交于 2021-02-04 19:52:11
问题 In Golang, after asserting to a slice, how is one able to remove an element from said slice? For example, the following returns the error cannot assign to value.([]interface {}) value.([]interface{}) = append(value.([]interface{})[:i],value.([]interface{})[i+1:]...) 回答1: If you have a slice value wrapped in an interface, you can't change it. You can't change any value wrapped in interfaces. When an interface value is created to wrap a value, a copy is made and stored in the interface. When

Why map and type assertion can return 1 or 2 values?

老子叫甜甜 提交于 2020-07-02 07:36:13
问题 To define a map, we can do such a thing: value, present := m["key"] or: value := m["key"] and with type assertion, we can do: var i interface{} = "hello" s := i.(string) fmt.Println(s) s, ok := i.(string) fmt.Println(s, ok) but I can't find a way to define a func that can return 1 value or 2-values. For instance: func hello() (string, error) { return "world", nil } When I invoke this func I get: v, ok := hello() // valid v := hello() // invalid PS: I know how something like template.Must

How to cast nil interface to nil other interface

蓝咒 提交于 2020-05-30 09:36:52
问题 I have a classic Go nil interface issue. I'm trying to assert an interface{} , which I assign from a nil error , back to an error interface. That sentence is confusing so I have a handy-dandy example: https://play.golang.com/p/Qhv7197oIE_z package main import ( "fmt" ) func preferredWay(i interface{}) error { return i.(error) } func workAround(i interface{}) error { if i == nil { return nil } return i.(error) } func main() { var nilErr error fmt.Println(workAround(nilErr)) // Prints "<nil>"

Typecast to a specific struct type by its string name

假如想象 提交于 2019-12-25 08:57:13
问题 I would like to typecast a specific variable to a specific defined struct/interface by using the string name value of the struct/interface. For example: type Testing interface{} and new variable stringName := "Testing" newTestingVariable.(stringName) Is this possible by chance? Perhaps using reflection? Cheers 回答1: It is not possible. Go is a statically typed language, which means types of variables and expressions must be known at compile-time. In a type assertion: x.(T) [...] If the type

Why does this Type Assertion on a direct implemented interface fail?

為{幸葍}努か 提交于 2019-12-25 00:53:09
问题 I am struggling with Go's Type Assertion mechanism. In the below example the Type Assertion for Qux.(Bar) fails. Why does a direct implementation of DoBar() at Qux not fullfill the Bar interface? package main import ( "fmt" ) type Nameable interface { Name() string } type Foo interface { Nameable DoFoo() string } type Bar interface { Nameable DoBar() string } type bar struct { name string } func (b bar) Name() string { return b.name } // Qux embeds bar and is expected to fullfill Nameable

invalid operation: type interface {} does not support indexing

白昼怎懂夜的黑 提交于 2019-12-13 20:26:46
问题 I'm new to the golang and I have problem while reading the nested JSON response. var d interface{} json.NewDecoder(response.Body).Decode(&d) test :=d["data"].(map[string]interface{})["type"] response.Body looks like this { "links": { "self": "/domains/test.one" }, "data": { "type": "domains", "id": "test.one", "attributes": { "product": " Website", "package": "Professional", "created_at": "2016-08-19T11:37:01Z" } } } The Error I'm getting is this: invalid operation: d["data"] (type interface

Detect value out of range errors using type assertion in Golang

核能气质少年 提交于 2019-12-08 07:16:21
问题 Given the following code: iv, err := strconv.ParseInt("18446744073709551448", 10, 64) fmt.Println(iv) fmt.Printf("%#v\n", err) fmt.Printf("%v\n", err) //Output: 9223372036854775807 &strconv.NumError{Func:"ParseInt", Num:"18446744073709551448", Err:(*errors.errorString)(0x1040a040)} strconv.ParseInt: parsing "18446744073709551448": value out of range How can I detect that the function failed due to being out of range of an int64? The strconv.ParseInt function returns an error type, but in this

Passing in a type variable into function

北城余情 提交于 2019-12-01 15:53:58
I'm trying to achieve a type assertion by passing in a type into a function. In other words, I'm trying to achieve something like this: // Note that this is pseudocode, because Type isn't the valid thing to use here func myfunction(mystring string, mytype Type) { ... someInterface := translate(mystring) object, ok := someInterface.(mytype) ... // Do other stuff } func main() { // What I want the function to be like myfunction("hello world", map[string]string) } What's the proper function declaration I need to use in myfunction , to successfully perform the type assertion in myfunction ?

Passing in a type variable into function

放肆的年华 提交于 2019-12-01 14:46:22
问题 I'm trying to achieve a type assertion by passing in a type into a function. In other words, I'm trying to achieve something like this: // Note that this is pseudocode, because Type isn't the valid thing to use here func myfunction(mystring string, mytype Type) { ... someInterface := translate(mystring) object, ok := someInterface.(mytype) ... // Do other stuff } func main() { // What I want the function to be like myfunction("hello world", map[string]string) } What's the proper function