- add func(a, b int) {}
- function run(a int, b int) {}
- func run(int a, b) {}
- func run(a, b int) {} CORRECT
func run(p Process, id1, id2 int) (pid int, err error) {}
- Inputs: p, id1, and id2. Results: pid, err. CORRECT
- Inputs: Process, int, int. Results: int, error.
- Inputs: run, p, id1, id2. Results: pid, err.
- The declaration syntax is wrong.
- Terminates a program.
- Terminates a func by returning zero or more values to a caller func. CORRECT
- Skips the next statement and runs the next.
func add(a, b int) {
return a + b
return
}
- The return statement should be called only once
- The last return statement is wrong
- It should declare an int result value and remove the last return statement CORRECT
- It should declare any numeric result value
2: Actually, it is correct because the func doesn't declare a result value.
3: Correct. It should be:
func add(a, b int) int { return a + b }
func incr(a int) {
a++
return
}
num := 10
// You want it to print 11 but it prints 10 instead.
fmt.Println( incr(num) )
- Change the func:
func incr(a int) int { a++; return a }
CORRECT - Change the func:
func incr(a int, newA int) { a++; newA = a }
- Change the func:
func incr(a int) int { return a++ }
1: Go is a 100% pass-by-value language. So, the inputs to a func are local to that function: The changes are not visible outside of that func.
- Nothing is wrong with them
- Funcs cannot use package level variables
- They may increase code coupling and cause fragile code CORRECT
3: It's because: Anyone can access and change them.
// Why this?
func incr(n string) (int, error) {
m, err := strconv.Atoi(n)
return n + 1, err
}
// Instead of this?
func incr(n string) int {
m, _ := strconv.Atoi(n)
return m + 1
}
- You want to let the caller know when something goes wrong CORRECT
- When an error occurs,
Atoi
returns 0, so you don't need to return an error
2: Sometimes, this is partly true however it is better to let the caller know when something goes wrong.
func spread(samples int, P int) (estimated float64) {
for i := 0; i < P; i++ {
estimated += estimate(i, P)
}
return
}
estimated
is a named result value. So the naked return returnsestimated
automatically. CORRECT- return statement is not necessary there. Go automatically returns
estimated
. - Result values cannot have a name. This code is incorrect.
IT SHOULD PRINT: map[1:11 10:3]
func main() {
stats := map[int]int{1: 10, 10: 2}
incrAll(stats)
fmt.Print(stats)
}
func incrAll(stats map[int]int) {
for k := range stats {
stats[k]++
}
}
- No, it doesn't work: Go is a pass by value language.
incrAll
cannot update the map value. - Yes, it works:
incrAll
can update the map value. CORRECT
2: Map values are pointers. So,
incrAll
can update the map value.
IT SHOULD PRINT: [10 5 2]
func main() {
stats := []int{10, 5}
add(stats, 2)
fmt.Print(stats)
}
func add(stats []int, n int) {
stats = append(stats, n)
}
- No, it doesn't work:
add()
cannot update the original slice header. CORRECT - Yes, it works:
add()
can add new element to the original slice header.
1: Go is a pass-by-value programming language. add() creates a copy of the original slice header and adds the new element to the new slice header but it never returns the updated one. So, it cannot update the original slice header. It should have been returning the original slice header.