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

How to use three points in Go

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use three points in Go". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use three points in Go.

'...' It's actually a grammatical sugar of go.

Its first use is mainly used in the case that the function has multiple uncertain parameters, and it can accept multiple uncertain parameters.

The second use is that slice can be broken up and passed.

Example:

Package main import ("fmt") func main () {name (1 test1 2) / / multiple uncertain number of parameters var strss= [] string {"qwr", "234", "yui", "cvbc",} test1 (strss...) / / slices are scattered into strss... Equivalent to "qwr", "234", "yui", "cvbc"} func name (args... int) {/ / can accept any int parameter for _, func test1 = range args {fmt.Println (v)}} func test1 (args... string) {/ / can accept any string parameter for _, VRV = range args {fmt.Println (v)}}

Let's take a look at the three dots (...) in Go. Usage, the details are as follows:

As we all know, the Go language is a strictly typed language, and what should we do if we encounter variable input parameters during development?

The three points here (… ), which brings us programmers a lot of flexibility, as follows

In Golang, three points are used in four places (what is the official term for three points?):

The code is as follows:

Package main import ("fmt"log") func main () {/ / multiParam can accept a variable number of arguments names: = [] string {"jerry", "herry"} / / in the array text,. The length specified by the symbol is equal to the number of elements in the text. Stooges: = [...] string {"Moe", "Larry", "Curly"} stoogxs: = [] string {"Moe", "Larry", "Curly"} strParam ("jerry", "herry") strParam (names...) / / merge two slice stoogxs = append (stoogxs, names...) via append Fmt.Println (stoogxs) / / identifies the number of elements in the array, here,... It means the number of elements in the array log.Println (len (stooges)) nums: = [] int {4,5,6} stoogns: = [...] int {4,5,6} intParam (1,2,3) intParam (nums...) / / identifies the number of array elements. It means that the number of elements of the array log.Println (len (stoogns))} func strParam (args... string) {/ / the accepted parameters are put in the args array for _, e: = range args {log.Println (e)}} func intParam (args... int) {res: = 0 for _, n: = range args {res + = n} log.Println (res)}

Variable length function parameter

If the last function argument is of type. T, then when calling this function, we can use several parameters of type T at the end of the argument list. Here, the type of T inside the function is actually [] T.

Func intParam (args... int) {res: = 0 for _, n: = range args {res + = n} log.Println (res)}

Call a function with a variable-length argument list

When the Sum function is called above, the variable length parameters are written separately. If we have a slice, then when we call it, we don't have to take the slice apart and call it again, just follow the slice. You can:

Nums: = [] int {4,5,6} intParam (nums...)

Identify the number of elements in the array

Stooges: = [...] string {"Moe", "Larry", "Curly"} / / len (stooges) = = 3

Here,. Means the number of elements in the array:

Wildcards in the Go command line

Wildcards that describe package files.

In this example, all packages in the current directory and all subdirectories are unit tested:

Go test. /... At this point, I believe you have a deeper understanding of "how to use the three points in Go". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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