Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang Tutorial Corrections: Slices, Functions #805

Open
MukuFlash03 opened this issue Aug 11, 2024 · 0 comments
Open

Golang Tutorial Corrections: Slices, Functions #805

MukuFlash03 opened this issue Aug 11, 2024 · 0 comments

Comments

@MukuFlash03
Copy link

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:

  • Length must be specified else error encountered.
  • Shorthand notation ':=' is to be used instead of '='.

  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:

  • Still need to use 'return'.
  • Named return just helps in skipping declaration step.
  • Also ensures all intended values are returned automatically.
    • No need to specify names but return still required.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant