Insert value into 'type abc interface{}'

WBOY
Release: 2024-02-10 20:39:17
forward
725 people have browsed it

将值插入“type abc interface{}”

php editor Xiaoxin is here to introduce to you a method of inserting values ​​into "type abc interface{}". In PHP, we often use interfaces to define a set of specifications. Sometimes, we encounter situations where we need to define a default value in the interface. At this time, we can use traits (features) to achieve this function. A trait is a piece of code that can be shared by multiple classes. By using traits, we can define default values ​​in the interface and use them in the class. This method is simple and easy to understand, let’s take a look at the specific implementation steps!

Question content

I am trying to insert a value into type abc [][] interface {}

I tried this:

insert := &abc{{0, {"abc", "def"}}}

This raises the error: Invalid compound literal type: interface {}

I also tried this:

insert, _ := json.Marshal([][]interface{}{{0, {"abc", "def"}}})

But this will also throw an error Invalid Composite Literal Type: interface{}

I want the output to be as follows:

[ [ 0, [ "abc", "def" ] ]

Can you let me know where I'm going wrong and how to fix this?

Workaround

abc's type allows any type, but the type of content you put in it needs to be defined. You have no type defined for {"abc", "def"} and you cannot instantiate an interface literal (interfaces don't tell the compiler anything about the fields). You can have anonymous structs, but you still need to define them.

Let's walk through what you have with an example:

x := &abc{}
Copy after login

Create an outer array with zero entries.

x := &abc{{}, {}, {}}
Copy after login

Create an outer array using 3 empty inner arrays.

x := &abc{{0, "abc"}}
Copy after login

An outer array with an inner array with 2 values: 0 and "abc".

x := &abc{{0, {"abc", "def"}}
Copy after login

An outer array, with an inner array, with 2 values: 0 and...what? This is an object literal but has no explicit type and no syntactically correct implicit type (it implicitly expects interface{} and cannot be instantiated directly), which is an error.

type pairofstrings struct {
  a string
  b string
}

x := &abc{{0, pairofstrings{"abc", "def"}}}
Copy after login

You have now entered the structure in question.

x := &abc{{0, struct {
  a string
  b string
}{"abc", "def"}}}
Copy after login

Anonymous structures are also an ugly but effective approach.

x := &abc{{0, []string{"abc", "def"}}}
Copy after login

seems to be the closest thing to what you want.

The rules are interface{} can save anything defined. It is not equivalent to C#'s dynamic object.

The above is the detailed content of Insert value into 'type abc interface{}'. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!