Skip to content

Commit 067115b

Browse files
committed
add variadic functions examples
1 parent d5a6b37 commit 067115b

File tree

23 files changed

+445
-1
lines changed

23 files changed

+445
-1
lines changed

.DS_Store

10 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

0-basic-variadic-example/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
say1("1", "2", "3", "\n")
7+
say1(append([]string{"1", "2", "3"}, append([]string{}, "4", "5", "6", "\n")...)...)
8+
9+
say2("1", "2", "3", "\n")
10+
args := []interface{}{"4", "5", "6"}
11+
say2(args...)
12+
}
13+
14+
func say1(args ...string) {
15+
for _, a := range args {
16+
fmt.Print(a)
17+
}
18+
}
19+
20+
// individual arguments can be of any type
21+
// destructured slice arguments have to be of type interface{}
22+
func say2(args ...interface{}) {
23+
fmt.Print(args...)
24+
}

1-cmd-no-variadic/.DS_Store

6 KB
Binary file not shown.

1-cmd-no-variadic/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
var commands = map[string]Command{}
4+
5+
func main() {
6+
// create new command
7+
command := cmd("get", "get todo by id", []string{"id"})
8+
command.Execute()
9+
10+
// get the existing command
11+
command = cmd("get", "", nil)
12+
command.Execute()
13+
}
14+
15+
func cmd(name, description string, args []string) Command {
16+
c, ok := commands[name]
17+
if ok {
18+
return c
19+
}
20+
c = Command{
21+
Name: name,
22+
Description: description,
23+
Args: args,
24+
}
25+
commands[name] = c
26+
return c
27+
}
28+
29+
type Command struct {
30+
Name string
31+
Description string
32+
Args []string
33+
}
34+
35+
func (c Command) Execute() {
36+
}

2-cmd-setter-getter/.DS_Store

6 KB
Binary file not shown.

2-cmd-setter-getter/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
var commands = map[string]Command{}
4+
5+
func main() {
6+
// create new command
7+
command := createCmd("get", "get todo by id", []string{"id"})
8+
command.Execute()
9+
10+
// get the existing command
11+
command = getCmd("get")
12+
command.Execute()
13+
}
14+
15+
func createCmd(name, description string, args []string) Command {
16+
c := Command{
17+
Name: name,
18+
Description: description,
19+
Args: args,
20+
}
21+
commands[name] = c
22+
return c
23+
}
24+
25+
func getCmd(name string) Command {
26+
c, ok := commands[name]
27+
if ok {
28+
return c
29+
}
30+
return Command{}
31+
}
32+
33+
type Command struct {
34+
Name string
35+
Description string
36+
Args []string
37+
}
38+
39+
func (c Command) Execute() {
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
var commands = map[string]Command{}
4+
5+
func main() {
6+
// create new command
7+
command := cmd(Args{
8+
Name: "get",
9+
Description: "get todo by id",
10+
Args: []string{"id"},
11+
})
12+
command.Execute()
13+
14+
// get the existing command
15+
command = cmd(Args{Name: "get"})
16+
command.Execute()
17+
}
18+
19+
type Args struct {
20+
Name string
21+
Description string
22+
Args []string
23+
}
24+
25+
func cmd(args Args) Command {
26+
c, ok := commands[args.Name]
27+
if ok {
28+
return c
29+
}
30+
31+
c = Command{
32+
Name: args.Name,
33+
Description: args.Description,
34+
Args: args.Args,
35+
}
36+
commands[args.Name] = c
37+
return c
38+
}
39+
40+
type Command struct {
41+
Name string
42+
Description string
43+
Args []string
44+
}
45+
46+
func (c Command) Execute() {
47+
}

4-cmd-variadic-all-optional/.DS_Store

6 KB
Binary file not shown.

4-cmd-variadic-all-optional/main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
var commands = map[string]Command{}
4+
5+
func main() {
6+
// create new command
7+
command := cmd("get", "get todo by id", "id")
8+
command.Execute()
9+
10+
// get the existing command
11+
command = cmd("get")
12+
command.Execute()
13+
}
14+
15+
func cmd(args ...string) Command {
16+
name, description, arguments := "", "", make([]string, 0)
17+
if len(args) > 0 {
18+
name = args[0]
19+
}
20+
c, ok := commands[name]
21+
if ok {
22+
return c
23+
}
24+
25+
if len(args) > 1 {
26+
description = args[1]
27+
}
28+
if len(args) > 2 {
29+
arguments = args[2:]
30+
}
31+
c = Command{
32+
Name: name,
33+
Description: description,
34+
Args: arguments,
35+
}
36+
commands[name] = c
37+
return c
38+
}
39+
40+
type Command struct {
41+
Name string
42+
Description string
43+
Args []string
44+
}
45+
46+
func (c Command) Execute() {
47+
}

0 commit comments

Comments
 (0)