4.2 KiB
4.2 KiB
What does the package keyword do in the following program?
package main
func main() {
}
- func
- package CORRECT
- fmt.Println
- import
- This keyword is used to declare a new function.
- That's right! package keyword allows you to define which package a Go file belongs to.
- This is not a keyword. It's the Println function of the fmt package.
- This keyword is used to import a package.
Which keyword is used to declare a new function?
- func CORRECT
- package
- Println
- import
What is a function?
- It's like a mini-program. It's a reusable and executable block of code. CORRECT
- It allows Go to execute a program.
- It allows Go to import a package called function.
- It prints a message to the console.
- Go looks for package main and func main to do that. A function doesn't do that on its own.
import
keyword does that.- For example:
fmt.Println
does that.
Do you have to call the main function yourself?
- Yes, so that, I can execute my program.
- No, Go calls the main function automatically. CORRECT
- No, you don't need to call the main function. Go automatically executes it.
Do you have to call the other functions yourself?
- Yes, so that, I can execute that function. CORRECT
- Yes, so that, Go can execute my program.
- No, Go calls the functions automatically.
- That's right. You need to call a function yourself. Go won't execute it automatically. Go only calls the main function automatically (and some other functions which you didn't learn about yet).
- That's only the job of the
func main
. There's only onefunc main
.
- Go doesn't call any function automatically except the main func (and some other functions which you didn't learn about yet). So, except the main func, you need to call the functions yourself.
What does package main
do?
package main
func main() {
}
- It controls everything
- It allows you to properly exit from a program
- It allows you to create an executable Go program CORRECT
What does func main
do?
package main
func main() {
}
- It contains a package called main
- Go starts executing your program by using the code inside func main CORRECT
- It prints a message to the console
- main function doesn't contain a package.
- That's right. Go automatically calls the main function to execute your program.
- It doesn't print anything at least directly.
What does import "fmt"
do?
package main
import "fmt"
func main() {
fmt.Println("Hi!")
}
- It prints "fmt" to the console
- It defines a new package called "fmt"
- It imports the
fmt
package; so you can use its functionalities CORRECT
fmt.Println
prints a message not theimport "fmt"
.package
keyword does that, not theimport
keyword.- Yes. For example, after you import the fmt package you can call its Println function to print a message to the console.
What this program does?
package main
func main() {
}
- It prints a message to the console
- It's a correct program but it doesn't print anything CORRECT
- It's an incorrect program
- It doesn't print a message. To do that you can use fmt.Println function.
- Yes, it's a correct program but since it doesn't contain fmt.Println it doesn't print anything.
- It's a correct program. It uses the package keyword and it has a main function. So, this is a valid and an executable Go program.
What does this program print?
package main
func main() {
fmt.Println(Hi! I want to be a Gopher!)
}
- Hi! I want to be a Gopher!
- It doesn't print anything
- This program is incorrect CORRECT
- It doesn't pass the message to Println wrapped between double-quotes. It should be like: fmt.Println("Hi! I want to be a Gopher")
- It doesn't import "fmt" package. Also see #1.
What does this program print?
package main
import "fmt"
func main() {
fmt.Println("Hi there!")
}
- Hi there! CORRECT
- fmt
- This program is incorrect; it imports the wrong package or there isn't a function called
Println
- import "fmt" imports the
fmt
package; so you can use its functionalities.- Actually, this program is correct.