Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of basic closure in Go language

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of basic closures in Go language. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

I. Closure Overview

Closure is a solution to the problem that local variables cannot be accessed externally

Closure is an application of function as return value

II. code demonstrates

The general idea is: define local variables inside the function, regard another function as the return value, local variables are equivalent to all variables for the return value function, so the value of local variables that call the return value function many times follows the change.

// closure.gopackage mainimport ( "fmt" "strings")func main() { f := closure("http://", "com") fmt.Printf("%T %p \n", f, f) fmt.Println(f("baidu")) fmt.Println(f("qq")) fmt.Println(f("alipay.com")) fmt.Println("") f1 := closure("http://", "com") fmt.Printf("%T %p \n", f1, f1) fmt.Println(f1("baidu")) fmt.Println(f1("qq")) fmt.Println(f1("alipay.com"))}func closure(prefix, suffix string) func(url string) string { pre, suf := prefix, fmt.Sprintf(".% s", suffix) return func(url string) string { fmt.Printf("%p %p ", &pre, &suf) if ! strings.HasPrefix(url, pre) { url = fmt.Sprintf("%s%s", pre, url) } if ! strings.HasSuffix(url, suf) { url = fmt.Sprintf("%s%s", url, suf) } return url }} Run Results $ go run closure.gofunc(string) string 0x4935600 xc0000301d0 0xc 0000301e0 http://baidu.com0xc0000301d0 0xc 0000301 e0 http://alipay.comfunc 0xc 0000301 e0 http://qq.com0xc0000301d0 (string) string 0x4935600 xc0000302 d0 0xc 000302 e0 http://qq.com0xc0000302d0 0xc 000302 e0 http://alipay.com Code Description http://baidu.com0xc0000302d0

Line 2 returns the type and address of the variable f function, which in Go is a reference type. If you have studied Java language, you know that Java object memory space has stack, heap, method area, static area.

Lines 3 - 5 return the addresses of the two variables pre suf, and each call to pre suf has the same address.

Lines 6 - 9 are the newly declared f1 variable, where the pre suf address is different from the pre suf address in the previous f.

Because when we create a variable that refers to a type, it is actually just a header value. The header value contains a pointer to the underlying data. The underlying structure it points to is not copied and passed. This also refers to the reason why type is more efficient than type passing.

About "Go language basic closure example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report