In this article we will learn about some of the frequently asked GO programming questions in technical like “go functions” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on go was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in GO. Below are some solution about “go functions” Code Answer’s.
xxxxxxxxxx
1
// a simple function
2
func functionName() {}
3
4
// function with parameters (again, types go after identifiers)
5
func functionName(param1 string, param2 int) {}
6
7
// multiple parameters of the same type
8
func functionName(param1, param2 int) {}
9
10
// return type declaration
11
func functionName() int {
12
return 42
13
}
14
15
// Can return multiple values at once
16
func returnMulti() (int, string) {
17
return 42, "foobar"
18
}
19
var x, str = returnMulti()
20
21
// Return multiple named results simply by return
22
func returnMulti2() (n int, s string) {
23
n = 42
24
s = "foobar"
25
// n and s will be returned
26
return
27
}
28
var x, str = returnMulti2()
xxxxxxxxxx
1
package main
2
3
import "fmt"
4
5
func basicFunction() {
6
fmt.Println("basic function golang")
7
}
8
9
var variableFunction = func() {
10
fmt.Println("basic function golang with variable function")
11
}
12
13
func parameterFunc(a, b int) {
14
addition := a + b
15
fmt.Println("total", addition)
16
}
17
18
func returningFunc(a, b int) int {
19
addition := a + b
20
return addition
21
}
22
23
func multipleReturningFunc(a, b int) (string, int) {
24
multiple := a + b
25
return "nilainya adalah", multiple
26
}
27
28
func multipleReturningNamingFunc(a, b int) (multiple, subtract int) {
29
multiple = a * b
30
subtract = a - b
31
return
32
}
33
34
func variadicFunction(array int) (sum int) {
35
36
for _, v := range array {
37
sum = v
38
fmt.Println(sum)
39
}
40
41
return
42
}
43
44
func anonymousFunc() {
45
name := "john doe"
46
func() {
47
fmt.Println(name)
48
}()
49
}
50
51
func anonymousParamsFunc() {
52
name := "jane doe"
53
func(str string) {
54
fmt.Println(str)
55
}(name)
56
}
57
58
func closureFunc() func() string {
59
name := "john"
60
return func() string {
61
name += "doe"
62
return name
63
}
64
}
65
66
func main() {
67
clousure := closureFunc()
68
69
basicFunction()
70
variableFunction()
71
Share this: