Intro Flashcards

1
Q

First statement of any go program

A

package
e.g. package main

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

Is package name a String?

A

No, package name is identifier

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

Executable vs Library programs in Go

A

Executable: These are compiled into executable files, which can be executed in future.
Library: These are consumed/called inside the executable program

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

Different characters included in “White space”

A
  1. Tabs
  2. Spaces
  3. New Line
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to consume code of other package inside our package?

A

Using “import” statement. The package name should always be inside the “double quote”.
e.g. import “fmt”

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

Types of comments in Golang

A

Single line: //
Multi line : /* */

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

Function declaration

A
  1. Start with keyword “func”
  2. Followed by function name
  3. Followed by parenthesis
  4. Followed by return type, nothing if void
  5. Followed by body
    e.g.
    func main() {
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to read documentation of library programs?

A

Using following command:
go doc packageName // with or without double quote

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

What is the full form of “fmt” package?

A

Formatted I/O

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

What are the different boolean values supported by Go?

A

Only two - “true” and “false” (both small case)

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

Is GO a statically typed language or Dynamically typed language ? What that means?

A

Go is a STATICALLY typed language, which simply means, it must know the “types” of all values before running the code.

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

Which function and package is required to find the type of any value ?

A

Package: “reflect”
Function: “TypeOf”

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

How to declare a variable in Go when we don’t know the value?

A

Use the keyword “var” followed by “name” of the variable followed by it’s “type”
»> var myVar int

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

How to initialize a variable during declaration and after declaration ?

A

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.

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

What will be the value of the variables, if we don’t initialize it?

A

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

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

What is the “short” variable declaration in Go-Lang?

A

“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”.

17
Q

Is it possible to just declare the variable without using it?

A

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

18
Q

What are the Naming RULES in Go-Lang?

A

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)

19
Q

Does Go supports “snake” case ?

A

No, we cannot use any special character in the name of variable/function/Type. Only Camel case is allowed.

20
Q

Can we perform math operation on multiple types ?

A

No, in Go, types must be same for any math operation. If Type is different then we need to do the “conversion”.

21
Q

How to convert int to float and vice-versa ?

A

Simply use the type, followed by parenthesis and then it’s value.
»> myInt := 12
»> myFloat := float32(myInt)