Intro Flashcards
First statement of any go program
package
e.g. package main
Is package name a String?
No, package name is identifier
Executable vs Library programs in Go
Executable: These are compiled into executable files, which can be executed in future.
Library: These are consumed/called inside the executable program
Different characters included in “White space”
- Tabs
- Spaces
- New Line
How to consume code of other package inside our package?
Using “import” statement. The package name should always be inside the “double quote”.
e.g. import “fmt”
Types of comments in Golang
Single line: //
Multi line : /* */
Function declaration
- Start with keyword “func”
- Followed by function name
- Followed by parenthesis
- Followed by return type, nothing if void
- Followed by body
e.g.
func main() {
}
How to read documentation of library programs?
Using following command:
go doc packageName // with or without double quote
What is the full form of “fmt” package?
Formatted I/O
What are the different boolean values supported by Go?
Only two - “true” and “false” (both small case)
Is GO a statically typed language or Dynamically typed language ? What that means?
Go is a STATICALLY typed language, which simply means, it must know the “types” of all values before running the code.
Which function and package is required to find the type of any value ?
Package: “reflect”
Function: “TypeOf”
How to declare a variable in Go when we don’t know the value?
Use the keyword “var” followed by “name” of the variable followed by it’s “type”
»> var myVar int
How to initialize a variable during declaration and after declaration ?
After declaration:
»> var myVar bool
»> myVar = true
During declaration
»> var myVar bool = true
OR
»> var myVar = true
During declaration we can omit the “TYPE” of the variable, as it will be automatically assigned by the value itself.
What will be the value of the variables, if we don’t initialize it?
It will be the ZERO value of the TYPE. The “Zero” value will be different for different types. Following are the Zero values of different types:
int -> 0
rune -> ‘’
string -> ““
float -> 0
bool -> false
What is the “short” variable declaration in Go-Lang?
“SHORT” variable declaration comes into the picture when we know the “value” of the variable during it’s declaration. In this we don’t need to mention the keyword “var” as well as “type” of the variable e.g.
»> var myVar int = 34 // long variable declaration
»> myVar := 34 // short variable declaration
The “semicolon” INDICATES the variable declaration. If we remove the “semicolon” it will become the “assignment”.
Is it possible to just declare the variable without using it?
No, in Go, there will be an compilation error, if we don’t use any package, import or variable. Only “declaration” is not allowed, it must be used somewhere
What are the Naming RULES in Go-Lang?
There are only two rules:
The first character of name of variable, function and Type must be started with a “letter”.
After first character, the other characters can be only “letters” and “numbers”
No special characters are allowed in the name
If the name of the variable/function/Type begins with “CAPITAL” letter, then it will be considered as “exported” and can be accessed outside the package (public)
If the name of the variable/function/Type begins with “SMALL” letter, then it will be considered as “private” and cannot not be accessed outside the package(private)
Does Go supports “snake” case ?
No, we cannot use any special character in the name of variable/function/Type. Only Camel case is allowed.
Can we perform math operation on multiple types ?
No, in Go, types must be same for any math operation. If Type is different then we need to do the “conversion”.
How to convert int to float and vice-versa ?
Simply use the type, followed by parenthesis and then it’s value.
»> myInt := 12
»> myFloat := float32(myInt)