Functions, Interfaces & Methods in Go

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/18

flashcard set

Earn XP

Description and Tags

Go

Go

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

What is a function ? Examples

  • A set of instructions with a name (name being optional).

  • main() is invoked automatically when the program runs.

  • Other functions have to be manually invoked.

2
New cards

What do functions support ?

  • Reusability (Declare once, call a million times, good for repetitive tasks)

  • Abstraction (You only need to know what the function does. Hiding details that are not important is abstraction)

<ul><li><p>Reusability (Declare once, call a million times, good for repetitive tasks)</p></li><li><p>Abstraction (You only need to know what the function does. Hiding details that are not important is abstraction)</p></li></ul><p></p>
3
New cards

Examples of how functions enable Reusability.

  • A database program may have a QueryDB() function.

  • A music app may have ChangeKey() function.

  • An image processing app may have a ThresholdImage() function.

4
New cards

Examples of how functions enable Abstraction.

knowt flashcard image
5
New cards

What are parameters and arguments ? Explain with the help of an example.

  • A function might need input data to perform compuation.

  • Parameters represent input data.

  • They must be mentioned inside the parentheses along with their types.

  • Arguments are passed and associated at the time of function call.

<ul><li><p>A function might need input data to perform compuation.</p></li><li><p><strong>Parameters </strong>represent input data.</p></li><li><p>They must be mentioned inside the parentheses along with their types.</p></li><li><p><strong>Arguments </strong>are passed and associated at the time of function call.</p></li></ul><p></p>
6
New cards

What are parameter options ?

  • If a function accepts no inputs, simply leave the parentheses empty.

  • If the function accepts consecutive inputs of the same type, just place the type at the end of the last parameter of that type.

<ul><li><p>If a function accepts no inputs, simply leave the parentheses empty.</p></li><li><p>If the function accepts consecutive inputs of the same type, just place the type at the end of the last parameter of that type.</p></li></ul><p></p>
7
New cards

What are return values ?

  • Results of computation

  • Type must be mentioned in the declaration after the parentheses.

  • Call can be placed on the RHS of a shorthand variable assignment.

<ul><li><p>Results of computation</p></li><li><p>Type must be mentioned in the declaration after the parentheses.</p></li><li><p>Call can be placed on the RHS of a shorthand variable assignment.</p></li></ul><p></p>
8
New cards

How to return multiple values ?

  • Comma separate the values returned.

  • In the function definition, comma separate the types of the return values in the same order as the return values themselves

<ul><li><p>Comma separate the values returned.</p></li><li><p>In the function definition, comma separate the types of the return values in the same order as the return values themselves</p></li></ul><p></p>
9
New cards

Explain functions as first class values in Go

  • Functions can be treated like any other type in Golang (like string, int, float)

  • They can be assigned to variables.

  • They can be passed as arguments to functions (functions can have functions as parameters) and returned from functions.

  • They can be stored in data structures like slices.

  • They can be created dynamically.

10
New cards

Give an example of how functions can be assigned to variables in Go

knowt flashcard image
11
New cards

Give an example of how functions can passed to other functions

knowt flashcard image
12
New cards

What are anonymous functions ? Explain with the help of an example

When functions are passed as arguments to other functions they need not have a name.

<p>When functions are passed as arguments to other functions they need not have a name.</p>
13
New cards

How are functions retuned from another functions ?

  • Possible due to closures

  • Used to create parameterized functions.

  • o_x and o_y are in the closure of fn

<ul><li><p>Possible due to closures</p></li><li><p>Used to create parameterized functions.</p></li><li><p><code>o_x</code> and <code>o_y</code> are in the closure of fn</p></li></ul><p></p>
14
New cards

What is an environment of a function ?

  • This concept is closely tied to lexical scoping in Go.

  • A set of all names (variable names for example) that can be accessed from a function.

<ul><li><p>This concept is closely tied to lexical scoping in Go.</p></li><li><p>A set of all names (variable names for example) that can be accessed from a function.</p></li></ul><p></p>
15
New cards

What is a closure ?

  • Function + environment data is called closure.

  • When a function is passed as an argument or returned from another function, it has access to the environment where it was declared.

  • This basically means that when a function is called it has access (it remembers) the environment data and has access to it at runtime.

  • 0,0 and 2,2 are remembered.

<ul><li><p>Function + environment data is called closure.</p></li><li><p>When a function is passed as an argument or returned from another function, it has access to the environment where it was declared.</p></li><li><p>This basically means that when a function is called it has access (it remembers) the environment data and has access to it at runtime.</p></li><li><p>0,0 and 2,2 are remembered.</p></li></ul><p></p>
16
New cards

What is variadic in Golang ?

  • A function in golang can accept variable number of arguments.

  • Use the … to specify this in the function. The single argument then contains all the arguments available as a slice.

<ul><li><p>A function in golang can accept variable number of arguments.</p></li><li><p>Use the … to specify this in the function. The single argument then contains all the arguments available as a slice.</p></li></ul><p></p>
17
New cards

How to pass a slice to a function with variable number of arguments ?

  • Use the … operator after the slice.

<ul><li><p>Use the … operator after the slice.</p></li></ul><p></p>
18
New cards

What is defer ?

  • A deferred function is one that is executed after the wrapping function is executed.

  • Typically used for cleanup purposes.

  • Implemented using the defer keyword placed in front of the call.

<ul><li><p>A deferred function is one that is executed after the wrapping function is executed.</p></li><li><p>Typically used for cleanup purposes.</p></li><li><p>Implemented using the defer keyword placed in front of the call.</p></li></ul><p></p>
19
New cards

How are arguments in the call deferred ?

The arguments are evaluated then and there. Only the call is deferred.

<p>The arguments are evaluated then and there. Only the call is deferred.</p><p></p>