This is a creation in Article, where the information may have evolved or changed.
Golang (officially released in 12) as a modern 10 language. The standard library of Golang provides a list of advanced data structures. Specific in the package container/list . The package consists of two data structures: "Element" and "List". where "Element" is equivalent to the "iterator" inside the CPP, its prev and next methods are used to get the previous or next iterator, the iterator's indirect reference directly uses its member value.
1 Create a list object to feel a bit
L Listen: = Listen to list. New () L.pushback ("one") L.pushback (2) fmt. PRINTLN (L)
The above code can get the following output:
&{{0x2081a21b0 Listen to 0x2081a21e0 listen <nil> listen to reflect. Value?} Listen 2}
Here the specific 0x data may change, the actual record is the address of the object, but the last "2" does not change, which means that the current list object contains two elements.
Here we use listen list. New () listens to create a list object and then calls the object's pushback () method inserts an element into the list object. Here we find a magical phenomenon: in CPP, the members of the list must be of the same type, but our Golang allows any type of member to be inserted into the list. It's easy to think of the features of Python. Here we can learn through the source element is actually stored in a listen to interface{} listens to members, thereby supporting the attributes of any member:
Type listen to the element listen to the struct listen {//listen to the value listen stored listen to Element.value listen to this listen to interface{}// Listen to contains listen to filtered listen to or listen to unexported listen to fields}
2. Traversing the list
The above example, only with FMT. println See the list is simple information, then to traverse the entire list how to do? Look at the code:
L Listen: = Listen to list. New () L.pushback ("one") L.pushback (2) l.pushback ("three") for listening to iter: = Listen L. Front (); ITER hear! = Listen nil listen; iter listen = listen to ITER. Next () Listen {Listen and listen to the FMT. Println ("Item:", ITER.) Value)}
Run the code and we can see the result:
Item: Listen to Oneitem: Listen to 2item: Listen to three
Here, we define an iterator variable, ITER, which is an element type, by invoking the list's listeningFront()function, you can get the first object of the list, and if the list is empty, you get nil. The same call to the list by invoking theBack()You can get an iterator to the last object. In the For statement, we use the list'sNext()method to get an iterator to the next element of the current iterator, or nil if there are no elements. So we used to listeniter !=nilAs the loop end condition. It says. By referencing the iterator'sValueMembers, you can implement the indirect application of the stored elements.
In addition to using the above function to get a single element, we can also get the number of its elements through the list Len() function. The drawback is that the Golang list does not provide the ability to reference elements in Python by index, and there are no index or at similar interfaces
3. Modifying a member of a list
In the example above, we've shown how to add elements to a list. pushback () adds a new element to the end of the list. In addition, there are pushfront () can insert an element into the list header. insertafter () can insert elements into the list after the specified element, insertbefor to insert an element in front of the specified element in the list.
How do I delete an element after inserting it? Through the list of remove () function can delete the specified element. moveafter () , movebefore () , you can specify the elements that follow or precede the elements in the list.
Let's look at an example. Combine these interfaces into an instance of a queue:
type listen to the queue listen to the struct listen {Listen to listen to the data listen to *list. List}func listen to Newqueue () listen to the *queue listen to listen to hear the Q listening to listen to listening to listen to the new (Queue) listen to hear q.data listen to listen to the list. New () Listen and hear return listen to Q}func (q listen *queue) Enqueue (v listen to interface{) Listen {Listen to hear Q.data.pushback (v)}func Listen (q listen *queue) Dequeue () Listen to interface{} Listen {Listen to the iter listen: = Listen to Q.data.front () listen to listen to the V listen: = Listen to ITER. Value listen and listen to Q.data.remove (ITER) listen to V}func listen to (Q listen to *queue) listen to dump () {Listen to hear for Listen to Iter:=q.data.front (); iter!=nil;iter= Iter. Next () Listen {Listen, listen, listen and listen to the FMT. Println ("Item:", ITER.) Value) Listen to listen to}}func listen to the main () {Listen to listen to listen to Q listen: = Listen to Newqueue () listen to listen to listen to the Q. Enqueue ("one") listen and listen to Q. Enqueue ("both") listen and listen to the Q. Dump () Listen and listen to the V listen: = listen to Q. Dequeue () Listen and listen to the FMT. Println ("V:", V) listen and listen to the Q. Dump ()}
We can see the result as:
Item: Listen to Oneitem: Listen to Twov: Listen to Oneitem: Listen to the
At last. If we want to clear the list. Can invoke its Init() listening interface.