Basics Flashcards

This covers material from the "Basics" section of the Go online tour (http://tour.golang.org).

1
Q

What is special about a name with a capital letter?

A

It is an EXPORTED name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What denotes an exported name?

A

It starts with a capital letter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Every Go program is made up of ________.

A

packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the name of the package where every program starts?

A

main

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What kind of statement is used to make external packages accessible?

A

import

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can the following statements be improved?

import "fmt" import "math"
A

By using a single, “factored” import statement:

import ( "fmt" "math" )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What kind of statement is used to define a function?

A

func

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can a function have zero arguments?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you define a function named “factorial”, taking an integer argument “x” and returning an integer?

A
func factorial(x int) int { // ... }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is there anything wrong with this function definition?

func odd (x int) bool { // ... }
A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is there anything wrong with this function definition?

func odd (int x) bool { // ... }
A

Yes; the argument type specifier must come AFTER the argument name: func odd (x int) ...

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Consider this function:

func add(x, y int) int { return x + y }

What are the types for x and y? Why?

A

Both x and y are type “int”. The int after the argument list x, y applies to both arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the special purpose of the package name “main”?

A

All programs start in this package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Assuming a package named “ez” with a function named “Action”, how could this function be accessed from another package?

A

By importing the package with import "ez", then using the full name ez.Action.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Assuming a package named “ez” with a function named “action”, how could this function be accessed from another package?

A

The function could not be accessed from outside the package because the name “action” does not start with a capital letter, thus it is not exported.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can a function return more than one result?

A

Yes

17
Q

How would you define a function named “swap”, taking two string arguments “x” and “y”, and returning two string arguments?

A
func swap(x, y string) (string, string) { // ... }
18
Q

How would you declare a package named “ez”?

A

With the statement package ez.

19
Q

Could the following function definition be improved?

func sum(x int, y int) int { // ... }
A

Yes, by shortening the argument declaration:

func sum(x, y int) int { // ... }
20
Q

Is there anything wrong with this package clause?

package "main"
A

Yes; the identifier main should not be quoted.

21
Q

Is there anything wrong with this package clause?

package main
A

No