- 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.
- Designing Purpose:Built Drones for Ardupilot Pixhawk 2.1
- Modern Web Testing with TestCafe
- Learning Windows Server Containers
- Mobile-first Bootstrap
- Ansible權威指南
- 嵌入式Linux系統開發:基于Yocto Project
- Linux性能優化
- 嵌入式應用程序設計綜合教程(微課版)
- 奔跑吧 Linux內核(入門篇)
- Hands-On UX Design for Developers
- Linux系統最佳實踐工具:命令行技術
- 鴻蒙操作系統設計原理與架構
- Raspberry Pi入門指南
- Windows PE權威指南
- Modern Python Cookbook