Variadic Parameter...https://play.golang.org/p/l6pbuFDz_eP

https://play.golang.org/p/l6pbuFDz_eP

What is the purpose of this line of code? “sum := 0”

Is it sort of like a starting place?

It declares and initialises sum to be an int of value 0.

Declare: tell the compiler that we want to use a variable with a certain type.
Initialise: assign a value the first time.

1 Like

I knew the meaning of := , but I didn’t know why 0.
Thanks for

Because if you were initialising sum to anything but 0, your results were off by exactly that amount.

1 Like

I think I’ve got it . but for some reason, my brain requires repeating before I truly get it to become a part of my thinking. Thank you In advance for your patience

Right.

I’m not certain how many times I will need to repeat this study. I’m sorry if it is aggravating to you. I will try to do it in private.

For some unknown reaso,n I don’t remember to check my notes.

Interesting that you don’t need to tell the type when you’re using the short declaration
operator.

It’s how our brain works. Things get into long term memory by repetition.

Some people need more of this repetition than other.

For me it has always been easier to talk to computers than talking with humans. For most people it seems to be the other way round.

Some people can build tables or other kind of furniture from a tree, I barely manage to carve a spear from a stick for my son.

Some people make a three course menu from ground meat, tomatoes and pasta, I burn the water, still my kids like my pasta :slight_smile:

It’s fine if some things take longer for you. Just stick to it. It will eventually “click” or at least you’ll be good enough to make your kids (or their kids) smile.

You’re making me laugh and laugh

I just had to read this again, as it’s quite delightful!

I just read it again.

I also get the points that you made.

You’re lucky!!! lol
I’m having a difficult time. My emotions don’t like it. But my mind and heart say, “Go on”.

In the following example

package main

import (  
    "fmt"
)

func find(num int, nums ...int) {  
    fmt.Printf("type of nums is %T\n", nums)
    found := false
    for i, v := range nums {
        if v == num {
            fmt.Println(num, "found at index", i, "in", nums)
            found = true
        }
    }
    if !found {
        fmt.Println(num, "not found in ", nums)
    }
    fmt.Printf("\n")
}
func main() {  
    find(89, 89, 90, 95)
    find(45, 56, 67, 45, 90, 109)
    find(78, 38, 56, 98)
    find(87)
}

I don’t understand these two statements.

found := false
found = true

As we have told you already quite a lot of times, := is used for “declare and initialise”, while = assigns to an already declared variable.

So please try to phrase with your own words, what you do not understand with these two statements?

1 Like

Let me analyze your answer. I don’t have a question about the short declaration operator now. It’s the use of false and true that I don’t understand.

It sets values. What is your question about those lines?

Try to go a step back, stop focussing on those 2 lines. Look at the function in it’s entirety. A single line does rarely do something significant, it gets significant and meaningful by looking at how individual lines of code interact with each other.

What will be the value of found at the end of the function if v was equal to num at least once? What will be its value if it never was equal?

What will be printed in which cases?

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.