copy(newSlice, origSlice)
duplicates rather than references- Therefore manipulation on the new slice will not change the original that it was copied from
-
keep
func()
args to 2, keepfunc
return values to 2, MAX 3!- if you need more look to making use of
Struct
- if you need more look to making use of
-
functions are first class citizens
-
functions are just like any other type
-
functions can be passed around just like any other type
-
when passing
fn
as an arg you don't need to execute it (eg.fn()
)- It is not asking for the return value that the function gives us, which is what you get when you execute it
- by passing the function you essentialy can use it later within whatever you are passing it to
func main() {
greet(sayHello) // No parens, just the func name
}
func greet(fn func() string) {
name := "Chris"
s := fn(name) // Here within, we actually fire it
fmt.Printf("Hello, %s", s)
}
func sayHello(name string) string {
// `Sprintf` returns a string
fmt.Sprintf("Hello, %s", name)
}
//-----------------
// If passing a func as a param gets complex
// - Break it out into a type
type greetFn func() string
// Greet then becomes
func greet(fn greetFn) {
name := "Chris"
s := fn(name) // Here within we actually fire it
fmt.Printf("Hello, %s", s)
}
Functions as params:
// other anonymous function examples
func main() {
// Predefined func set on the spot
fn := func() string {
return "I'm a string"
}
// Variable to another existing function
// - Called without the parens it refers to the func rather than calling it
fnTwo := sayHello
// we can use it if greets() arg type is a
// function that returns a string
// Like this ---> func greet(argFn func() string) {}
greet(fn)
// Same as above, just refers to a pre-created function `sayHello`
greet(fnTwo)
// Inline function call. Same as our var fn above but rather
// then setting it to the variable and using the var we
// just put it right in as the argument
greet(func() string {
return "I'm a string"
})
// You would go with an inline func(above) if you didn't need the
// variable reference. Only create the var if it gives clarity or
// is used in multiple places.
}
func sayHello(name string) string {
fmt.Sprintf("Hello, %s", name)
}
func greet(fn func() string) {
name := "Chris"
s := fn(name) // Here within we actually fire it
fmt.Printf("Hello, %s", s)
}
-
&
denotes a Pointer -
Normally when you pass something into a func it copies it
- This means within the function it does not reference the original value passed in, it is its own new thing with a new memory address
-
Pointers essentially instruct the opposite to happen, they say "don't copy this, I want to be able to work with the original item tied to this var"
-
*
denotes that the item expects to receive a pointer that was defined by&
a := &SomeThing
// a is a variable that holds a Pointer to "SomeThing"
func gimmePointers(arg *SomeThing) {
//...
}
// "gimmePointers()" is a func that takes an arg. The arg type is saying with
// "I expect what you are passing me (`arg`) to be a pointer to an item with
// the type `SomeThing`" which is shown by the asterisk
- Github PR template