Anonymous functions and closures of Golang

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

The go language supports anonymous functions, where functions can be passed or used like normal variables.

Here's how to use it:

Main.go

Package Mainimport ("FMT") Func main () {varV Func (Aint)intv= Func (Aint)int {        returnAA} fmt. Println (V (6))//two ways to do itV1: = Func (iint)int {        returnIi} fmt. Println (v1 (7))}

The anonymous function of the go language is closures, and the following is the explanation of closures in go language programming

  Basic concepts
Closures are blocks of code that can contain variables that are free (not bound to specific objects), that are not defined within the code block or
in any global context, but are defined in the context in which the code block is defined. The block of code to execute (because the free variable contains
in the code block, so these free variables and the objects they refer to are not freed) provide a binding calculation ring for the free variable
boundary (scope).
  Closure Value
The value of a closure is that it can be used as a function object or as an anonymous function, which means, for a type system, not only that the
data also represents the code. Most languages that support closures use functions as first-level objects, meaning that they can be stored in
variables as arguments to other functions, and most importantly, can be dynamically created and returned by a function.

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.