Tenth lesson Go language function

Source: Internet
Author: User

1 built-in functions

The Len () function can accept different type parameters and return the length of that type .

If we pass in a string, the length of the string is returned ,

If an array is passed in, the number of elements contained in the array is returned.

2 Custom Functions

 //  function returns a single value  func Max (A, b int ) int   { if  a > b { return   A}  else      { return   b }}  //  function returns multiple values  func Swap (A, B int ) (int , int   ) { return   B, a}  

3 value passing and reference passing

var int 1 Ten     Swap2 (&aa, &BB)    FMT. PRINTLN (AA)    *int, b *int) () {    varint      = *a    *a = *b    *b = Temp}

4 function methods

A method is a function that contains the recipient, which can be either a value of a named type or struct type or a pointer. All methods of the given type belong to the method set of that type

Package Main

Import "FMT"

Type Circle struct { //define struct body
Radius float64
}

Func Main () {
var c Circle
C.radius = 10.00
Fmt. Println ("area of the C is", C.getarea ()) //method that can directly invoke the struct body

}

Func (c Circle) Getarea () float64 { //This method is a struct-body method
Return 3.14*c.radius*c.radius
}

5 function closures

Tenth lesson Go language 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.