Variable Declaration
1) var (
a int
arr []int
)
2) var a int
Slices in Golang
letters := []string{"a", "b", "c", "d"}
sample := [][]int{{1, 2, 3}, {4, 5, 6}}
1D Slice
Data field in slice header is pointer to the underlying array. For a one dimensional slice, we have
below declaration
oneDSlice := make([]int, 2)
To declare a two dimensional slice the declaration would be
2D
twoDSlice := make([][]int, 2)
Above declaration means that we want to create a slice of 2 slices. Carefully understand this point. But wait a second here, we haven't specified the second dimension here, meaning what is the length of each of the inner 2 slices. In case of slice, each of the inner slice has to be explicitly intialised like below
for i := range twoDSlice {
twoDSlice[i] = make([]int, 3)
}
Array
b := [...]string{"Penn", "Teller"}
b := [2]string{"Penn", "Teller"}
var a [4]int
a[0] = 1
i := a[0]
sample := [2][3]int{{1, 2, 3}, {4, 5, 6}}
sample := [2][3]byte{}
Calculation length of array
// Print number of items
fmt.Println("Length:", len(array))
String to int operation
The most common numeric conversions are Atoi (string to int) and Itoa (int to string).
i, err := strconv.Atoi("-42")
s := strconv.Itoa(-42)
Take Input -
fmt.Println("Enter a number")
var num int
fmt.Scanln(&num)
Priority Queue
Priority Queue is an extension of queue with following properties.
- Every item has a priority associated with it.
- An element with high priority is dequeued before an element with low priority.
- If two elements have the same priority, they are served according to their order in the queue.
Structures
A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct.
is go object oriented or functional
Go (or “Golang”) is a post-OOP programming language that borrows its structure
(packages, types, functions) from the Algol/Pascal/Modula language family. Nevertheless, in Go, object-oriented patterns are still useful for structuring a program in a clear and understandable way.
Input / Output Operation
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
//TAKING INPUT THROUGH BUFIO
func main() {
(1)
var string mystring
reader := bufio.NewReader(os.Stdin)
mystring, _ := reader.ReadString('\n')
fmt.Println(mystring)
(2)
reader := bufio.NewReader(os.Stdin)
mystring, _ := reader.ReadString(' ')
fmt.Println(mystring)
(3)
reader := bufio.NewReader(os.Stdin)
score, _ := reader.ReadString('\n')
myscore, _ := strconv.ParseFloat(strings.TrimSpace(score), 64)
fmt.Println(myscore + 10)
reader := bufio.NewReader(os.Stdin)
test, _ := reader.ReadString('\n')
t, _ := strconv.ParseInt(strings.TrimSpace(test), 10, 64)
fmt.Println(t + 10)
//(4)
reader := bufio.NewReader(os.Stdin)
myfirststring, _ := reader.ReadString(' ')
mysecondstring, _ := reader.ReadString('\n')
fmt.Println(myfirststring + mysecondstring)
//reader := bufio.NewReader(os.Stdin)
myfirststring, _ = reader.ReadString(' ')
mysecondstring, _ = reader.ReadString('\n')
fmt.Println(myfirststring + mysecondstring)
}
Comments
Post a Comment