- Distributed Computing with Go
- V.N. Nikhil Anurag
- 214字
- 2021-06-24 18:36:08
variadic.go
In order to understand the first set of tests, we need to understand what a variadic function is and how Go handles it. Let's start with the definition:
Given that Go is a statically typed language, the only limitation imposed by the type system on a variadic function is that the indefinite number of arguments passed to it should be of the same data type. However, this does not limit us from passing other variable types. The arguments are received by the function as a slice of elements if arguments are passed, else nil, when none are passed.
Let's look at the code to get a better idea:
// variadic.go package main func simpleVariadicToSlice(numbers ...int) []int { return numbers } func mixedVariadicToSlice(name string, numbers ...int) (string, []int) { return name, numbers } // Does not work. // func badVariadic(name ...string, numbers ...int) {}
We use the ... prefix before the data type to define a functions as a variadic function. Note that we can have only one variadic parameter per function and it has to be the last parameter. We can see this error if we uncomment the line for badVariadic and try to test the code.
- 30天自制操作系統
- Linux設備驅動開發詳解(第2版)
- Containerization with LXC
- Learning Windows Server Containers
- FreeRTOS實時內核應用指南
- Kali Linux滲透測試全流程詳解
- SharePoint 2013 應用開發實戰
- 數據中心系統工程及應用
- Linux自動化運維:Shell與Ansible(微課版)
- Mastering Reactive JavaScript
- NetDevOps入門與實踐
- HTML5 Enterprise Application Development
- 大規模分布式系統架構與設計實戰
- iOS 10快速開發:18天零基礎開發一個商業應用
- Java EE 8 High Performance