Go Lang Flashcards
Is GO a compiled or interpreted language?
Go is a compiled language
Go is either compiled or interpreted. What is a benefit or feature that GO provides that is unlike other languages executed similarly to GO?
Go, although being a compiled language, still has a garbage collector, which makes memory management easy
GO doesn’t have classes what does it have in its stead and what are some of the features of classes that GO is not able to implement?
GO doesn’t have classes but has structs instead. Structs don’t support inheritance, constructors, and generics. This makes it easier to code and faster to run.
Are variables case-sensitive in GO?
yes they are
How to declare variables in GO?
var x, y int
keyword var_name_comma_sep type
What’s a way to initialize and declare variables together in GO?
you can either do
var x int =100
or
var x = 100 //infers that 100 is int and assigns accordingly
or
x := 100 // can only be done inside a function; its illegal outside
What are * and & in reference to pointers?
The “*” operator is used to declare a pointer variable and to access the value stored at the memory address pointed by the pointer.
The “&” operator is used to get the memory address of a variable. It is also called the address-of-operator.
func main() {
x := 10 // Declare variable x with value 10
var ptr int // Declare a pointer variable ptr of type int
ptr = &x // Assign the memory address of x to ptr
fmt.Println(“x:”, x) // Prints x: 10
fmt.Println(“ptr:”, ptr) // Prints ptr: 0x10414120
fmt.Println(“ptr:”, *ptr) // Prints *ptr: 10
*ptr = 20 // Change the value stored at the memory address pointed by ptr
fmt.Println(“x:”, x) // Prints x: 20
}
what does the new() function do?
It returns a pointer to a variable.
use case:
ptr := new(int)
*ptr = 3
Does dynamically and statically allocated memory go in a heap or stack?
Dynamically allocated memory in a heap and statically allocated memory in the stack.
what is iota in GoLang?
iota is used when you want to define a set of constants which each have a different value but you don’t really care about what the value is.
what’s is the typical form of a switch statement
switch variable {
case value1:
// code to be executed if variable == value1
case value2:
// code to be executed if variable == value2
case value3:
// code to be executed if variable == value3
…
default:
// code to be executed if variable does not match any of the case values
}
what happens in Go if you write a switch statement without a tag?
it executes the first case that evaluates to true
what is scan in Go and how is it used
scan is a method provided by the fmt package in Go, which is used to read input from a user and parse it into variables. It is commonly used to read input from the standard input (stdin)
how do you define a slice in GoLang
for slices using [] is main
var my_slice[]int
or
my_slice := []int
or
weekdays := make([]string, 5)
or []string
or
[]string{}
or
[]string{value1, value2, value3, …value n}
arr := [7]string{“This”, “is”, “the”, “tutorial”, “of”, “Go”, “language”}
first.go
package main
import “fmt”
func main() {
var a1 byte = 97 var a2 byte = 98 var a3 byte = 99 fmt.Println(a1) fmt.Println(a2) fmt.Println(a3) fmt.Printf("%c\n", a1) fmt.Printf("%c\n", a2) fmt.Printf("%c\n", a3) }
$ go run first.go
97
98
99
a
b
c
A byte in Go is an unsigned 8-bit integer. It has type uint8. A byte has a limit of 0 – 255 in numerical range. It can represent an ASCII character.
Go uses rune, which has type int32, to deal with multibyte characters.