- 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.
- Cybersecurity:Attack and Defense Strategies
- Persistence in PHP with the Doctrine ORM
- BPEL and Java Cookbook
- 構建可擴展分布式系統:方法與實踐
- Linux自動化運維:Shell與Ansible(微課版)
- 嵌入式系統及其應用(第三版)
- Joomla! 3 Template Essentials
- Mastering Reactive JavaScript
- jQuery UI Cookbook
- Windows Server 2012網絡操作系統項目教程(第4版)
- Advanced TypeScript Programming Projects
- Windows 7實戰從入門到精通(超值版)
- iOS 10 開發指南
- Docker容器技術與應用
- Raspberry Pi入門指南