GoTheBiggerPicture Flashcards
Main problems solved by Go-Lang
- In-efficient Compilation
- In-efficient Execution
- Complex programming model
From Which language syntax is inspired in Go lang?
‘C Language’
Is Go procedural or Object Oriented ?
It is both.
Is Go Garbage collected ?
Yes Go language automatically performs garbage collection.
what it means when we say Go is fully compiled language?
It means, the Go compiled code doesn’t requires a “mediator” runtime to execute the same. It can be directly executable by the OS.
Unlike other languages like Java or Python, which requires a “mediator” runtime to execute the compiled code, the Go doesn’t requires any runtime same.
Does Go provides Single Binary output after compilation ?
yes, to make deployment easier, the Go compiler provides single binary output for deployment.
When first version of Go was released and what’s its release cycle ?
First Version: 2012 - 1.0
Release Cycle: Every six Month
Current Version: 1.20
What are the go inbuilt packages which provide network supports?
net and net/http
Which package in Go language provides “concurrency” support ?
goRoutine
How can we share resources between concurrent threads in Go ?
Using “channels”
How Go lang generate OS specific binaries for Windows, OS X and Android?
Go uses following variables to create Platform specific binaries:
1. GOOS
2. GOARCH
Once we set these environment variables, Go automatically creates the OS specific binaries. e.g.
1. Windows: GOOS: windows & GOARCH: amd64
2. OSX: GOOS: darwin & GOARCH: amd64
3. Android: GOOS: android & GOARCH: arm
Which package in GO is used to accept command line parameters ?
“Flag”
What should be the first statement of any Go Source File ?
“package” statement.
It says where our source code sits in our Application.
How to put a single line comment in Go ?
// This is a commented line
How to put a multi-line comment in Go ?
/*
This is
multi line comment.
*/
What is an Import statement ?
Import statement is used to import “external” package inside our package.
What is an Import block ?
An import block allowed us to import multiple packages without repeating the word “import”. e.g.
import (
“os”
“fmt”
)
What type of indentation is used by Go formatter to format the code - “Space” or “Tab”?
“Tab”.
It is a standard practice to use “Tab” for indentation, instead of “spaces”
Do GO compiler throws error, if indentation is not provided by the user ?
No
Indentation should be used for code readability but it is not mandatory.
Is it required to use semicolon at the end of each statement ?
No
Go compiler replaces the “new line” character with “semicolon”, so that it is not required by the user to put it at the end of each statement.
What is wrong with following code ?
func main()
{
fmt.Println(“hello”)
}
The line “func main()” will be considered as a “statement”, hence Go compiler will put a Semicolon after this. Hence after compilation it will be look like this
func main();
{
fmt.Println(“hello”)
}
Hence we will get two errors for this:
1. function main without body
2. Semicolon is missing
what is the sub command used for getting the “version” ?
go version
In above “go” is the command and “version” is the subcommand
How to find out documentation about any “subcommand” in Go ?
“help” subcommand.
»> go help doc
What is done by “run” subcommand ?
- Compiles the code
- Create a temporary “executable file” current directory
- Executes the “temporary executable file”
- clean the local workspace
What is a Go Module ?
Go Module is a directory which contains the “go.mod” file. It can be seen as “workspace” or “project directory”.
How to initialise a go module ?
go mod init <module></module>
What is the standard format to name a Go Module ?
- <repository-domain>/<user-name>/<repository-name>
</repository-name></user-name></repository-domain> - All Lower Case
e.g. githib.com/fil/my-repository
What is the default content of go.mod file ?
- Module Name
- Go Version
How to run an entire Module in Go ?
Use module identifier:
go run github.com/my-name/my-repository
What is the keyword required to declare a variable ?
“var”
What is the order of providing type in Variable declaration ?
The Type is always provided at the end of declaration in the format
var <variableName> <variableType> e.g.
var i int</variableType></variableName>
How to declare and initialise variables in a “single line” with data type ?
var <variableName> <dataType> = <value>
e.g. var f float32 = 3.14</value></dataType></variableName>
What is the shorthand way to declare and initialise variables without using the “var” keyword and data type
<variableName> := <variableValue>
e.g. firstName := "arthur"
</variableValue></variableName>
How to do multi variable declaration and initialisation ?
This can be only done by using “Shorthand syntax”.
e.g. i, j = 12, 23
(Two names on left side and two values on right side)
What’s a pointer variable ?
A variable which stores only “address” of another variable.
How to declare a pointer variable ?
By Using “*” with variable type
e.g. var ptr *string
What is the default value of pointer variable without initialisation ?
“nil”
How to initialise an empty pointer variable ?
Using “new” function:
var ptr *string = new(string)
What is “referencing” and “de-referencing” ?
Referencing: It means pointing to the address of variable
de-referencing: It means pointing to the “value” of the variable
How to access the value of a variable, if we have it’s memory address (pointer) ?
By using Dereferencing variable.
e.g fmt.Println(*ptr)
How to assign a value to the variable, if we have it’s memory address (pointer) ?
By using Deferencing variable:
e.g. *ptr = “myValue”
How to save address of a variable into pointer variable ?
Using “address” variable:
e.g. ptr := &myvar
What is a constant ?
A variable whose value cannot be changed
How to declare a constant implicitly and explicitly ?
Implicit Declaration = Type determined as per the value of constant
e.g. const i = 23
Explicit Declaration = Type provided by the programmer
e.g. const i int = 23
?What’s the advantage of implicit const declaration ?
In Implicit declaration, the compiler automatically changes the type of the varaible based on the operation:
e.g. const i = 2
i + 2 // int + int
i + 2.3 // int + float
In second operation, the compiler will automatically converts the constant into float type and then performs the operation
What’s a constant block ?
A constant block is a package level block, where we can declare and intialize multiple constants without repeating the word “constant”
e.g const (
first = 23
second = 24
)
what is iota keyword ?
It’s just a “int” constant sequencing keyword. It increments everytime we declare and use constants.