Skip to content

Commit 717aa59

Browse files
committed
init
1 parent f50a485 commit 717aa59

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
.idea

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module github.com/golang-infrastructure/go-variable-parameter
2+
3+
go 1.18
4+

variable_parameter.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package variable_parameter
2+
3+
// TakeFirstParam 获取可变参数的第一个参数
4+
func TakeFirstParam[T any](parameters []T) T {
5+
if len(parameters) > 0 {
6+
return parameters[0]
7+
} else {
8+
var zero T
9+
return zero
10+
}
11+
}
12+
13+
// TakeFirstParamOrDefault 获取可变参数的第一个参数,如果没有传的话则使用默认值
14+
func TakeFirstParamOrDefault[T any](parameters []T, defaultValue T) T {
15+
if len(parameters) > 0 {
16+
return parameters[0]
17+
} else {
18+
return defaultValue
19+
}
20+
}

0 commit comments

Comments
 (0)