Golang Study notes: Experience function closures

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

Recently idle to have no matter, will graduate, sold oneself to Hangzhou's recruit Silver network, just use this time can study the following go language well.

From high school began to contact programming, then the use of Pascal language, mainly to participate in the information contest, later went to university, sophomore began to learn Java began my Java journey. To now, has been used in Java for nearly six years, the previous period of time to find a job deeply realized the beauty of Java-the great demand. Haha, compared to the lab in C/s, the small partners, really is too happy!!!

Good base friends After graduating to the United States excellent products, two years ago and I mentioned Golang, said is very promising, then there is no feeling, also did not take the matter. Until some time ago, I heard that B station and some companies, back-end system with Golang reconstruction, coupled with the emergence of blockchain, feel the future even if Golang is not the most cock, it will be the mainstream.

To tell the truth, re-learn a language, say simple is simple, say not simple is not simple. Recently, mainly follow the Go Guide, step by step to understand the familiar go language (fortunately did not forget clean C language!!!) )

In the guide, the previous exercises are basically no more difficult until the function closure section.

Returns the number of consecutive Fibonacci numbers using a function closure.

Initial state

package mainimport "fmt"// fibonacci 函数会返回一个返回 int 的函数。func fibonacci() func() int {}func main() {f := fibonacci()for i := 0; i < 10; i++ {fmt.Println(f())}}

Seems difficult, my idea is: to maintain a queue length of 2, each return will be the number of two in the queue, and the first number out of the team, the results are then queued.

The first thing to solve is the implementation of the queue. There are many tools in Java that can implement a queue, so what about go? Almighty Google tells me that you can implement a queue with container/list.

The list of pushback, remove, front can meet my needs.

When I was actually writing, I encountered an awkward question: the elements in the list were element types, and go did not specify the specific type of the element as Java did when creating the list, but instead used the legendary universal type interface.

When I wanted to add two elements, I got an error:

Cannot convert A.value (type interface {}) to type Int:need type assertion

The solution is also clear: a type assertion is required

By Google Discovery, type assertions are a mechanism for solving such problems in Golang : A Type assertion provides access to an interface value ' s underlying Concrete value. The type assertion mechanism provides the interface value to the specific type value of the conversion interface

For my needs, you can use type assertions to implement the conversion of interface values to int values.

All that's left is to fulfill the demand.

package mainimport ("fmt""container/list")// fibonacci 函数会返回一个返回 int 的函数。func fibonacci() func() int {fl:=list.New()fl.PushBack(int(0))fl.PushBack(int(0))return func() int {a:=fl.Front()fl.Remove(a)b:=fl.Front()tmp:=int(a.Value.(int))+int(b.Value.(int))//类型断言if tmp == 0{tmp =1;}fl.PushBack(tmp)return tmp;}}func main() {f := fibonacci()for i := 0; i < 10; i++ {fmt.Println(f())}}

Just started to learn go, some of the concepts may not understand the wrong, I hope you help point out!!!

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.