Basics Flashcards
This covers material from the "Basics" section of the Go online tour (http://tour.golang.org).
What is special about a name with a capital letter?
It is an EXPORTED name.
What denotes an exported name?
It starts with a capital letter.
Every Go program is made up of ________.
packages
What is the name of the package where every program starts?
main
What kind of statement is used to make external packages accessible?
import
How can the following statements be improved?
import "fmt" import "math"
By using a single, “factored” import statement:
import ( "fmt" "math" )
What kind of statement is used to define a function?
func
Can a function have zero arguments?
Yes
How would you define a function named “factorial”, taking an integer argument “x” and returning an integer?
func factorial(x int) int { // ... }
Is there anything wrong with this function definition?
func odd (x int) bool { // ... }
No
Is there anything wrong with this function definition?
func odd (int x) bool { // ... }
Yes; the argument type specifier must come AFTER the argument name: func odd (x int) ...
Consider this function:
func add(x, y int) int { return x + y }
What are the types for x and y? Why?
Both x and y are type “int”. The int
after the argument list x, y
applies to both arguments.
What is the special purpose of the package name “main”?
All programs start in this package.
Assuming a package named “ez” with a function named “Action”, how could this function be accessed from another package?
By importing the package with import "ez"
, then using the full name ez.Action
.
Assuming a package named “ez” with a function named “action”, how could this function be accessed from another package?
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.