Go language variable parameter (varying function parameter)

  Suitable variable parameters used, so that the code can be simple to use, especially the input and output class function, such as a log function or the like.

  This article from Example 4 describes the use of variable parameters.

  code:

package main

import (
    "bytes"
    "fmt"
)

/****************** 1 ****************
Variable parameters may be defined function
... shaped like the type of format type only as a function of the type of the parameter is present,
And it must be the last argument, it is a syntactic sugar (syntactic sugar),
That this syntax of the language does not affect the function, but more programmers to use,
In general, the use of syntactic sugar can increase the readability of the program, thereby reducing the possibility of errors program
*/
func myfunc(args ...int) {
    for _, v := range args {
        fmt.Println (v)
    }
}

/****************** 2 ****************
Any type of variable parameters
Any type of data transfer interface {} is customary usage in Go, the use of interface {} is still safe type
*/
func MyPrintf(args ...interface{}) {
    for _, arg := range args {
        switch arg.(type) {
        case int:
            fmt.Println(arg, "is an int value.")
        case string:
            fmt.Println(arg, "is an string value.")
        case int64:
            fmt.Println(arg, "is an int64 value.")
        default:
            fmt.Println(arg, "is an unknown value.")
        }

    }
}

/****************** 3 ****************
Traversing the variable argument list - get the value for each parameter
* / 
FUNC JoinStrings (slist ... String ) String {
     // definition of a byte buffer, quick connect string 
    var B for bytes.Buffer states
     for _, S: = Range slist {
         // The write byte string traversed byte array 
        b.WriteString (s)

    }
    // The ligated byte array into a string 
    return b.String ()
}

func rawPrint(slst ...interface{}) {
    for _, v := range slst {
        fmt.Println (v)
    }
}

/****************** 4 ****************
Passing a plurality of variable parameters in the parameter function
If you pass this variable contains the variable parameters of the function to the next variable parameter, the variable parameter can be added back to the variable transmission when ...
*/
func personPrint(slst ...interface{}) {
    rawPrint(slst...)
}

func main() {

    // define a function variation of parameters 
    myfunc ( . 1 , . 7 , . 5 )

    var V1 int = . 1 
    var V2 Int64 = 234 
    var V3 String = " Hello " 
    var = V4 float32 1.23 
    // any type of variable parameters 
    myprintf (V1, V2, V3, V4)
     // iterate variable argument list - Get the value of each parameter 
    fmt.Println (JoinStrings ( " Ladies " , " and " , " Gentlemen " ))
     // passing parameters in a plurality of variable parameters functions 
    personPrint ( . 1 , . 3 , . 5 )
}

 

  Program output:

1
7
5
1 is an int value.
234 is an int64 value.
hello is an string value.
1.23 is an unknown value.
ladies and gentlemen
1
3
5

 

Guess you like

Origin www.cnblogs.com/personblog/p/12319366.html