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

The usage of variable Parameter function in Go language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "the usage of variable parameter function in Go language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the use of variable parameter functions in Go language".

The function in Go supports variable arguments, which is really just a syntax candy of the Go language. The format is as follows:

Func function (values...Type) {} / / Type: indicates the specific parameter type, which can be int,string... Even interface.

Example 1: variable parameters are of the same type

As can be seen from the above code, the functions with variable parameters support the input methods of the parameters in 4. They are:

1. Do not input parameters; 2. Enter a parameter; 3. Input multiple parameters; 4. Enter as slice.

The output results can be calculated normally for these different inputs.

Note: for the same type of variable parameter method, the input parameter is similar to the slice input method of [] type, but they are somewhat different, as shown below:

The compiler still defines these two incoming parameters as different functions as to whether they are variable parameters or slice parameters, as shown in the output above.

Example 2: variable parameters are different types

When the variable parameter is a different data type, the type of the variable parameter must be set to empty interface.

Code analysis: it can be seen from the code that when the type of variable parameter is interface {}, arguments can be different types of data. Two different ways of passing parameters are shown in the code, one is to pass different types of arguments directly, and the other is to build slice into interface {} to pass arguments.

Output analysis: through the output, we can see that the two ways of input parameters are valid, and each corresponding input parameter type is the data type of the argument we passed.

At this point, I believe you have a deeper understanding of "the use of variable parameter functions in the Go language". 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

Internet Technology

Wechat

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

12
Report