ronreiter / interactive-tutorials

Interactive Tutorials
Apache License 2.0
4.04k stars 2.56k forks source link

Golang Tutorial Corrections: Slices, Functions #805

Open MukuFlash03 opened 1 month ago

MukuFlash03 commented 1 month ago

Found some incorrect explanations, syntax in the Golang tutorial.


  1. Slices
Screenshot 2024-08-11 at 1 04 47 AM

Code:

package main

import "fmt"

func main () {
    // Add your code here.
    exampleSlice = make([]int)
    fmt.Println(exampleSlice)
}

Errors:

./prog.go:7: undefined: exampleSlice
./prog.go:7: missing len argument to make([]int)
./prog.go:8: undefined: exampleSlice

Corrections needed:


  1. Functions
Screenshot 2024-08-11 at 1 08 03 AM

Code:

package main

import "fmt"

func add (a int, b int) (sum int) {         // here we are defining the variable name of what we are returning
    sum = a + b                             // so no need for a return statement, go takes care of it
}

func main() {
    sum := add(3, 5)

    fmt.Println(sum)                // prints 8
}

Errors:

./prog.go:7: missing return at end of function

Corrections needed: