variables and declaration Flashcards

(8 cards)

1
Q

Variable Declaration (Explicit)

A

var name string = “Alice”
var age int = 25
Explanation:

var keyword declares a variable.

Explicitly specifies type (string, int, etc.).

Can be used inside and outside functions.

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

Short Variable Declaration (Inferred Type)

A

name := “Bob” // string
age := 30 // int
Explanation:

:= infers the type automatically.

Only works inside functions.

Preferred for concise code.

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

Zero Values (Default Values)

A

var a int // 0
var b string // “”
var c bool // false
Explanation:

Uninitialized variables get default zero values.

int: 0, string: “”, bool: false.

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

Multiple Variable Declaration

A

var x, y int = 10, 20
a, b := “hello”, true
Explanation:

Declares multiple variables in one line

Works with both var and :=

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

Type Conversion

A

i := 42
f := float64(i) // Convert int to float64
Explanation:

Go requires explicit type conversion.

Syntax: Type(value).

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

Constants (Immutable Values)

A

const Pi = 3.14
const Greeting = “Hello”
Explanation:

const declares unchangeable values.

Must be initialized at declaration.

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

Variable Scope

A

package main

var globalVar = “I’m global!”

func main() {
localVar := “I’m local!”
}
Explanation:

Global: Declared outside functions (accessible everywhere in the package).

Local: Declared inside functions (accessible only within that function).

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

Blank Identifier (_)

A

_, err := someFunction() // Ignore the first return value
Explanation:

_ discards unwanted values (avoids compiler errors).

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