Variablels Flashcards
Declare a variable in go
use keyword “var” followed by “name” followed by “type”
e.g. var myString string
End of statement in go-lang
- By using new line character
2. By using semicolon
Declare variable with datatype
var myVar string = “asdfa”
Declare variable without datatype
var myVar = "stasdf" // here the type is identified using the literal
Declare variable without var keyword
myVar := "asdf" // here the type is identified using the literal
What is lexical scoping of variables in Go?
It means, the variable can be accessed only inside the curly braces where it was declared.
is it going to work? myVar := "asd" func main(){ fmt.Println("hello world" + myVar) }
No, outside the body function, only “non-declarative” statements are allowed.
What is created here?
const myVar string = “asdfasf”
We created a constant here.
Is there any difference between scoping of a constant and a variable?
No, there is no difference between them.
Is it correct syntax?
var ( a = 23; b = “asd”; c= 234.23)
Yes, here we are creating multiple variables using single var keyword
What this function will do?
fmt.Scanf()
This will read input from standard inputs.
How many loops are there in Go-lang?
Only one, for loop
Syntax of for loop?
for { // all codes goes here }
Is it necessary to provide for expression inside a parenthesis?
No
Syntax of if-else
if { // if block } else { // else block }