The design pattern of Go learning article

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > <span style= "FONT-SIZE:14PX;" > In the Pure object-oriented design language, the design pattern for most of us should be the most familiar with the most powerful weapon, but in the go language, from the contact so far in the past half a year has not yet encountered the concept of Go design mode, these days to think up also to turn over the information, Find out about Go design patterns related information, but very few, even the simplest single-case mode, I think for a long time also do not know how to write with Go language, because there is no static concept go, is very worried. Today, we are studying the other people's writing about go iterator, in fact, is completely imitate the Java C # language iterator, but also very reference value, at least for us to open a good head:</span></span>


The following code is mostly derived from: Http://www.tuicool.com/articles/vumYbu

Goiterator Project Main.gopackage mainimport "FMT" type Ints []intfunc (i Ints) First () Iterator {return iterator{data:< C0/>i,index:0,}}type Iterator struct {data  intsindex int}func (i Iterator) Get () int {return I.data[i.index]}func (i Iterator) Hasnext () bool {return I.index < Len (i.data)}func (i Iterator) Next () Iterator {return Iterator{data:  I.D Ata,index:i.index + 1,}}func Main () {ints: = Ints{1, 2, 3}for it: = INTs. First (); It. Hasnext (); it = it. Next () {fmt. Println (it. Get ())}}
We are more familiar with Java's Integer, here is to imitate the integer class to do the go iterator encapsulation

Goiterator Project Main.gopackage mainimport "FMT" type Ints []intfunc (i Ints) Iterator () func () (int, bool) {index: = 0return func () (val int, OK bool) {if index >= len (i) {return}val, OK = I[index], Trueindex++return}}func main () {INTs : = Ints{1, 2, 3}it: = Ints. Iterator () for {val, ok: = it () if!ok {break}fmt. Println (val)}}

anonymous functions and closures are used here to achieve the effect of iterators, but this is not a good place to break iterators, or to have iterators output the results we want at some point, but the idea is really new.

Goiterator Project Main.gopackage mainimport "FMT" type Ints []intfunc (i Ints) Iterator () <-chan int {c: = Make (chan int) go func () {for _, V: = range I {c <-v}close (c)} () return C}func main () {ints: = Ints{1, 2, 3}for V: = Range ints.i Terator () {fmt. Println (v)}}

the above uses channel to complete iterative traversal int[] Each element of the function, although seemingly gorgeous, it is really gorgeous, but the same as above, not very good interruption, otherwise it will bring permanent blocking ... and switching between threads will have a certain amount of CPU consumption, but can be ignored, the idea is worthy of reference. Having seen these, I think the design pattern in the Go application, will certainly let us see not the same magical power!


If you have any good go design mode Welcome to share Oh! -* _*-



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.