├── Go_Basics ├── arrays.go ├── conditionalLogic.go ├── constants.go ├── dateAndTime.go ├── defer.go ├── errors.go ├── functions.go ├── hello-world.go ├── interface.go ├── loops.go ├── map.go ├── mathadvanced.go ├── mathsimple.go ├── memoryManagement.go ├── methodOfCustomType.go ├── multiReturnFunctionsInGo.go ├── pointers.go ├── readFile.go ├── slice.go ├── string.go ├── stringsAndOutput.go ├── struct.go ├── switchInGo.go ├── takeInputAdvanced.go ├── takeInputNumber.go ├── takeInputSimple.go ├── variables.go ├── walkDirectory.go ├── webRequest.png └── writeToFile.go └── README.md /Go_Basics/arrays.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | 9 | var colors [3]string 10 | colors[0] = "Red" 11 | colors[1] = "Green" 12 | colors[2] = "Orange" 13 | fmt.Println(colors) 14 | fmt.Println(colors[0]) 15 | 16 | /* array declaration and assignment together */ 17 | var numbers = [4]int{7, 23, 49, 34} 18 | fmt.Println(numbers) 19 | fmt.Println(numbers[3]) 20 | } 21 | -------------------------------------------------------------------------------- /Go_Basics/conditionalLogic.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | /* Go is sensitive to whitespaces 8 | put else in same line as ending braces of if statement 9 | 10 | if-else is same as java etc except that parenthesis is not needed*/ 11 | 12 | func main() { 13 | // var x float64 = 43 14 | var result string 15 | 16 | if x := 43; x < 0 { 17 | result = "x is smaller than 0" 18 | } else if x == 0 { 19 | result = "x is equal to 0" 20 | } else { 21 | result = "x is greater than 0" 22 | } 23 | 24 | fmt.Println("Result is : ", result) 25 | } 26 | -------------------------------------------------------------------------------- /Go_Basics/constants.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | //constant explicit typed 9 | const aNumber int = 23 10 | const aString string = "This is Go - const explicit" 11 | 12 | //const implicit typed 13 | const aNumber = 23 14 | const aString = "This is Go! - const implicit" 15 | } 16 | -------------------------------------------------------------------------------- /Go_Basics/dateAndTime.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | 10 | /* Date and Now are members of the package time 11 | you can't use them with objects created by you*/ 12 | t := time.Date(2000, time.May, 26, 10, 45, 0, 0, time.UTC) 13 | fmt.Printf("I saw the world on %s\n", t) 14 | 15 | now := time.Now() 16 | fmt.Printf("Time now is : %s\n", now) 17 | 18 | /* Functions for the time object */ 19 | fmt.Println("The month is : ", t.Month()) 20 | fmt.Println("The Day is : ", t.Day()) 21 | fmt.Println("The weekday is : ", t.Weekday()) 22 | 23 | /* Add date OR 24 | append date by one using AddDate Function*/ 25 | nextDay := t.AddDate(0, 0, 1) 26 | fmt.Printf("Next Date After 26 May 2000 is : %v %v, %v (%v)\n", 27 | nextDay.Day(), nextDay.Month(), nextDay.Year(), nextDay.Weekday()) 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Go_Basics/defer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | /* defer as the words is self-explanatory 8 | wherever any statement is marked as defer that is postponed 9 | */ 10 | 11 | /* within a function if any statement is marked as defer then that is shown by the end of function 12 | when all other statements are already executed*/ 13 | func main() { 14 | defer fmt.Println("Defer 1 - It is a deferred statement- will be printed in last") 15 | fmt.Println("Not deferred statement") 16 | myFunc() 17 | defer fmt.Println("Defer 2 - It is a deferred statement- will be printed in second last") 18 | fmt.Println("Defer statements are printed in LIFO manner like stack. \nSo last deferred statement in a function willl be shown first") 19 | 20 | } 21 | 22 | func myFunc() { 23 | fmt.Println("I will be executed after 1st non deferred statement of main method") 24 | } 25 | -------------------------------------------------------------------------------- /Go_Basics/errors.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | /* os is a package and Open() is a method in it that 11 | tries to open given files from the os*/ 12 | file, err := os.Open("noName.txt") 13 | 14 | if err == nil { 15 | fmt.Println(file) 16 | } else { 17 | fmt.Println(err) 18 | } 19 | 20 | /* Create your own error object */ 21 | myErrorObject := errors.New("My new error object") 22 | fmt.Println(myErrorObject) 23 | 24 | /* Handle error using ok*/ 25 | attendance := map[string]bool{ 26 | "Saumya": true, 27 | "Ambika": false, 28 | "Jyotsna": true} 29 | 30 | isPresent, ok := attendance["Saumya"] 31 | if ok { 32 | fmt.Println("Saumya is Present?", isPresent) 33 | } else { 34 | fmt.Println("No information about Saumya") 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Go_Basics/functions.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | firstFunction() 9 | sum := 0 10 | sum = functionWithParam(10, 90) 11 | fmt.Println("Sum : ", sum) 12 | 13 | sum = addAllValues(90, 50, 10, 20) 14 | fmt.Println("New Sum : ", sum) 15 | } 16 | 17 | func firstFunction() { 18 | fmt.Println("First Function in Go") 19 | } 20 | 21 | /* function with argument and return type int */ 22 | func functionWithParam(value1 int, value2 int) int { 23 | return value1 + value2 24 | } 25 | 26 | /*You can create functions that expect arbitrary number of values 27 | as long as they all are of same types */ 28 | func addAllValues(values ...int) int { 29 | sum := 0 30 | 31 | /* add all values like you do in slice */ 32 | for i := range values { 33 | sum += values[i] 34 | } 35 | /* print type of slice */ 36 | fmt.Printf("%T\n", values) 37 | return sum 38 | } 39 | -------------------------------------------------------------------------------- /Go_Basics/hello-world.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | func main() { 9 | fmt.Println("Hello From GO!") 10 | fmt.Println(strings.ToUpper("Loud and Clear!")) 11 | } 12 | -------------------------------------------------------------------------------- /Go_Basics/interface.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type Animal interface { 8 | Speak() string 9 | } 10 | 11 | type Dog struct { 12 | Name string 13 | } 14 | 15 | /* custom type Dog is implementation of interface Animal 16 | since speak () is a method of type structure */ 17 | 18 | func (d Dog) Speak() string { 19 | return "Barkkk!" 20 | } 21 | 22 | func main() { 23 | /* wrapped the Dog object of structure around Interface Animal */ 24 | poodle := Animal(Dog{"Teddy"}) 25 | fmt.Println(poodle) 26 | } 27 | -------------------------------------------------------------------------------- /Go_Basics/loops.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | 9 | /* slice named colors */ 10 | colors := []string{"red", "green", "blue"} 11 | fmt.Println(colors) 12 | 13 | sum := 0 14 | for i := 0; i < 10; i++ { 15 | sum += i 16 | } 17 | fmt.Println("sum : ", sum) 18 | 19 | for i := 0; i < len(colors); i++ { 20 | fmt.Println(colors[i]) 21 | } 22 | 23 | fmt.Println("expecting same result as above") 24 | 25 | /* range will set the value of i to the current index */ 26 | for i := range colors { 27 | fmt.Println(colors[i]) 28 | } 29 | 30 | /* try break, continue and labels */ 31 | sum = 1 32 | for sum < 1000 { 33 | 34 | sum += sum 35 | fmt.Println(sum) 36 | if sum > 200 { 37 | goto endofprogram 38 | } 39 | if sum > 500 { 40 | break 41 | } 42 | } 43 | endofprogram: 44 | fmt.Println("End of Program") 45 | } 46 | -------------------------------------------------------------------------------- /Go_Basics/map.go: -------------------------------------------------------------------------------- 1 | /* map in go is an unordered collection of key value pair 2 | its very common to use strings for keys and any other type for associated value*/ 3 | 4 | package main 5 | 6 | import ( 7 | "fmt" 8 | "sort" 9 | ) 10 | 11 | func main() { 12 | /* creating a map called languages */ 13 | languages := make(map[string]string) 14 | languages["one"] = "Java" 15 | languages["two"] = "C++" 16 | languages["three"] = "Python" 17 | languages["four"] = "Javascript" 18 | 19 | fmt.Println(languages) 20 | 21 | /* variables to print java */ 22 | variableToPrintJava := languages["one"] 23 | fmt.Println(variableToPrintJava) 24 | 25 | /* delete a key value pair e.g key three */ 26 | delete(languages, "three") 27 | fmt.Println(languages) 28 | 29 | /* Iterating over map key and value */ 30 | fmt.Println("Iterating over map key and value pair") 31 | for k, v := range languages { 32 | fmt.Printf("%v : %v \n", k, v) 33 | } 34 | 35 | /* Sorting maps result using SLICES */ 36 | /* when iterating iver range keyword if you only assign one variable you 37 | will only get the key and not value*/ 38 | 39 | keys := make([]string, len(languages)) 40 | i := 0 41 | for k := range languages { 42 | keys[i] = k 43 | i++ 44 | } 45 | 46 | /* sort the slice named keys alphabetically */ 47 | sort.Strings(keys) 48 | fmt.Println("Print the slice : ", keys) 49 | // fmt.Println("Now print sorted map") 50 | for i := range keys { 51 | fmt.Printf("%v \n", languages[keys[i]]) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Go_Basics/mathadvanced.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "math/big" 7 | ) 8 | 9 | func main() { 10 | /* big.float is a data type, give more precise output 11 | this is explicit declaration*/ 12 | 13 | var b1, b2, b3, sum big.Float 14 | b1.SetFloat64(2.5) 15 | b2.SetFloat64(7.5) 16 | b2.SetFloat64(3.2) 17 | 18 | sum.Add(&b1, &b2).Add(&sum, &b3) 19 | fmt.Printf("Big Sum better precision %.10g\n", &sum) 20 | 21 | /* the verb .2f means output should contain only 22 | 2 characters after 23 | the decimal */ 24 | circleRadius := 18.5 25 | circumference := circleRadius * math.Pi 26 | /* printf takes verbs like .2 f etc, println doesn't */ 27 | fmt.Printf("Circumference: %.2f\n", circumference) 28 | } 29 | -------------------------------------------------------------------------------- /Go_Basics/mathsimple.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | 9 | /* implicit declaration of variables */ 10 | int1, int2, int3 := 4, 2, 6 11 | intSum := int1 + int2 + int3 12 | fmt.Println("Int Sum :", intSum) 13 | 14 | float1, float2, float3 := 2.5, 4.5, 6.5 15 | floatSum := float1 + float2 + float3 16 | fmt.Println("Float Sum :", floatSum) 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Go_Basics/memoryManagement.go: -------------------------------------------------------------------------------- 1 | /* new and make are two imp functions in terms of memory 2 | 3 | new () function : allocate but doesn't initialize memory 4 | make () function : allocate and initialize memory */ 5 | 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | ) 11 | 12 | func main() { 13 | 14 | /* declaration of objects e.g map without make function can cause panic 15 | example is below : 16 | */ 17 | 18 | var m map[string]int 19 | m["key"] = 23 20 | fmt.Println(m) 21 | 22 | /* above code snippet produce error 23 | panic: assignment to entry in nil map 24 | */ 25 | 26 | /* if you intent to immediately add data to the map then declare 27 | it using make()*/ 28 | m := make(map[string]int) 29 | m["key"] = 423 30 | fmt.Println(m) 31 | 32 | /* Memory de allocation 33 | memory is de allocated using GC - garbage collector 34 | */ 35 | } 36 | -------------------------------------------------------------------------------- /Go_Basics/methodOfCustomType.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type Dog struct { 8 | Name string 9 | Weight int 10 | Sound string 11 | } 12 | 13 | /* methods of custom type e.g structure 14 | method of type structure (dog)*/ 15 | /* these functions don't accept params, neither do they return any value */ 16 | func (d Dog) Speak() { 17 | fmt.Println(d.Sound) 18 | } 19 | 20 | func main() { 21 | poodle := Dog{"Ticy", 40, "Woof"} 22 | fmt.Println(poodle) 23 | poodle.Speak() 24 | } 25 | -------------------------------------------------------------------------------- /Go_Basics/multiReturnFunctionsInGo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | n1, l1 := FullName("Helen", "Keller") 9 | fmt.Printf("Full Name is %v , total length is %v \n", n1, l1) 10 | 11 | n2, l2 := FullNameNakedReturn("B.K.", "Shivani") 12 | fmt.Printf("Full Name is %v , total length is %v \n", n2, l2) 13 | } 14 | 15 | /* order in which params are written in func signature should 16 | be same as at time of return in end */ 17 | func FullName(f, l string) (string, int) { 18 | fullName := f + " " + l 19 | length := len(fullName) 20 | return fullName, length 21 | } 22 | 23 | /* This way is called naked Return in which after return statement nothing else us needed */ 24 | func FullNameNakedReturn(f, l string) (fullName string, length int) { 25 | fullName = f + " " + l 26 | length = len(fullName) 27 | return 28 | } 29 | -------------------------------------------------------------------------------- /Go_Basics/pointers.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | /* pointers are the variables that stores the address of other variables 9 | p pointer is pointing to an int variable 10 | */ 11 | var p *int 12 | 13 | if p != nil { 14 | fmt.Println("The value of p : ", *p) 15 | } else { 16 | fmt.Println("The value of p : nil") 17 | } 18 | 19 | var v int = 23 20 | p = &v 21 | if p != nil { 22 | fmt.Println("The value of p : ", *p) 23 | } else { 24 | fmt.Println("The value of p is nil") 25 | } 26 | 27 | var floatVariable float64 = 23.5 28 | pointer1 := &floatVariable 29 | fmt.Println("Value of pointer1 : ", *pointer1) 30 | 31 | /* lets change the value of pointer1 32 | */ 33 | *pointer1 = *pointer1 / 10 34 | fmt.Println("Updated value of pointer1 : ", *pointer1) 35 | fmt.Println("Value of floatVariable : ", floatVariable) 36 | /* both the values are same or simultaneously updated because of the line pointer1 := &floatVariable 37 | it establishes common connection btw the pointer and variable */ 38 | } 39 | -------------------------------------------------------------------------------- /Go_Basics/readFile.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | ) 7 | 8 | func main() { 9 | fileName := "./map.go" 10 | 11 | /* this function Readfile returns a byte array */ 12 | content, err := ioutil.ReadFile(fileName) 13 | checkError(err) 14 | 15 | /* converting byte array into string */ 16 | result := string(content) 17 | fmt.Println("Content of file: \n", result) 18 | } 19 | 20 | func checkError(err error) { 21 | if err != nil { 22 | panic(err) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Go_Basics/slice.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | /* slice, very similar to array should have all data of same types 9 | * but unlike array it is resizable */ 10 | 11 | func main() { 12 | /* size is not defined in slices unlike arrays */ 13 | var colors = []string{"Blue", "Green", "Yellow"} 14 | fmt.Println(colors) 15 | 16 | /* append items in slice using append function */ 17 | colors = append(colors, "Orange") 18 | fmt.Println(colors) 19 | 20 | /* remove First element of slice using append method */ 21 | /* colors = append(colors[1:]) is same as colors = append(colors[1:len(colors]) 22 | blank is by default taken as last*/ 23 | colors = append(colors[1:]) 24 | fmt.Println(colors) 25 | 26 | /* remove Last element of slice using append method */ 27 | /* colors = append(colors[:len(colors)-1]]) is same as colors = append(colors[0:len(colors)-1]]) 28 | blank is by default taken as the very first element*/ 29 | colors = append(colors[:len(colors)-1]) 30 | fmt.Println(colors) 31 | 32 | /* declare slice using make function and assign values later */ 33 | numbers := make([]int, 5, 5) 34 | numbers[0] = 23 35 | numbers[1] = 7 36 | numbers[2] = 99 37 | numbers[3] = 89 38 | numbers[4] = 2 39 | fmt.Println(cap(numbers)) 40 | 41 | numbers = append(numbers, 999) 42 | fmt.Println(numbers) 43 | 44 | /* print capacity of slice */ 45 | fmt.Println(cap(numbers)) 46 | 47 | /* sort slice in ascending order*/ 48 | sort.Ints(numbers) 49 | fmt.Println(numbers) 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Go_Basics/string.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | func main() { 9 | 10 | /* 11 | string in Go can treated in 2 ways 12 | 1. as a simple single value 13 | 2. array of bytes 14 | */ 15 | 16 | str1 := "This is implicitly typed" 17 | var str2 string = "This is explicitly typed" 18 | fmt.Println("Value -> ", str1) 19 | 20 | fmt.Printf("Value is : %v\nType is : %T\n", str1, str1) 21 | fmt.Printf("Value is : %v\nType is : %T\n", str2, str2) 22 | 23 | /* ToUpper and Title are the functions in package strings */ 24 | fmt.Println(strings.ToUpper(str1)) 25 | fmt.Println(strings.Title(str1)) 26 | /* output of strings.ToUpper : THIS IS IMPLICITLY TYPED 27 | output of strings.Title : This Is Implicitly Typed 28 | */ 29 | 30 | // check equality 31 | lValue := "hello" 32 | uValue := "HELLO" 33 | fmt.Println("Equal Case Sensitive?", lValue == uValue) 34 | fmt.Println("Equal Non-Case Sensitive", strings.EqualFold(lValue, uValue)) 35 | 36 | // contains substring? 37 | fmt.Println("Contains substring?", strings.Contains(str1, "expli")) 38 | fmt.Println("Contains substring?", strings.Contains(str2, "expli")) 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Go_Basics/stringsAndOutput.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | string1 := "i am learning go," 8 | string2 := "go is compiled language," 9 | string3 := "golang is easy and has some OOPs Features" 10 | 11 | fmt.Println(string1, string2, string3) 12 | } 13 | -------------------------------------------------------------------------------- /Go_Basics/struct.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | /* struct is a data structure in go 8 | similar to C's struct and Java classes 9 | 10 | It doesn't have inheritance model like super or substruct etc */ 11 | 12 | type Flower struct { 13 | Name string 14 | Color string 15 | PetalNumber int 16 | } 17 | 18 | func main() { 19 | 20 | sunFlower := Flower{"SunFlower", "Yellow", 50} 21 | rose := Flower{"Rose", "Red", 80} 22 | fmt.Println(sunFlower) 23 | fmt.Println(rose) 24 | 25 | /* detailed info about the structure object */ 26 | fmt.Printf("%+v\n", sunFlower) 27 | 28 | /* separately get the objects feature */ 29 | fmt.Printf("Name : %v, Color : %v\n", rose.Name, rose.Color) 30 | } 31 | -------------------------------------------------------------------------------- /Go_Basics/switchInGo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func main() { 10 | 11 | /* the first line seeds the randomized value using a number of milliseconds */ 12 | rand.Seed(time.Now().Unix()) 13 | dow := rand.Intn(6) + 1 14 | result := "" 15 | 16 | switch dow { 17 | case 1: 18 | result = "It is sunday" 19 | case 7: 20 | result = "It is saturday" 21 | default: 22 | result = "It is weekday" 23 | } 24 | fmt.Println("Day of Week is : ", dow) 25 | fmt.Println("Result : ", result) 26 | 27 | x := -42 28 | switch { 29 | case x < 0: 30 | result = "x is less than zero" 31 | case x == 0: 32 | result = "x is equal to zero" 33 | default: 34 | result = "x is greater than zero" 35 | } 36 | fmt.Println(result) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Go_Basics/takeInputAdvanced.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | //create a reader object 11 | //bufio.NewReader will read the input from the input console 12 | reader := bufio.NewReader(os.Stdin) 13 | fmt.Println("Enter text please: ") 14 | 15 | //a function with variable str that returns an error object. error object is presented by _ 16 | // \n in single quotes means bytes not strings 17 | str, _ := reader.ReadString('\n') 18 | fmt.Println(str) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Go_Basics/takeInputNumber.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strconv" 8 | "strings" 9 | ) 10 | 11 | func main() { 12 | 13 | //onject to read input console 14 | reader := bufio.NewReader(os.Stdin) 15 | fmt.Println("Enter Number: ") 16 | str, _ := reader.ReadString('\n') 17 | 18 | f, err := strconv.ParseFloat(strings.TrimSpace(str), 64) 19 | if err != nil { 20 | fmt.Println(err) 21 | } else { 22 | fmt.Println("Number is : ", f) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Go_Basics/takeInputSimple.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | //variable s of type string has been declared 9 | var s string 10 | 11 | fmt.Scanln(&s) 12 | fmt.Println(s) 13 | } 14 | -------------------------------------------------------------------------------- /Go_Basics/variables.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | 9 | //explicitly typed declaration 10 | var aNumber int = 23 11 | var aString string = "This is Go!- explicit" 12 | 13 | //implicitly typed declaration 14 | aNumber := 23 15 | sString := "This is Go! - implicit" 16 | } 17 | 18 | // bool string int byte 19 | -------------------------------------------------------------------------------- /Go_Basics/walkDirectory.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "path/filepath" 7 | ) 8 | 9 | func main() { 10 | 11 | /* this method Abs() returns 2 values 12 | so we are keeping two values*/ 13 | root, _ := filepath.Abs(".") 14 | fmt.Println("Visited Path :", root) 15 | 16 | /* this method walkFile returns error object */ 17 | err := filepath.Walk(root, visitedPath) 18 | if err != nil { 19 | fmt.Println("error object is", err) 20 | } 21 | } 22 | 23 | /* this method returns error object and accept 3 param 24 | 3 params are - file path as string, info of the file and error object*/ 25 | func visitedPath(path string, info os.FileInfo, err error) error { 26 | if err != nil { 27 | return err 28 | } 29 | 30 | if path != "." { 31 | if info.IsDir() { 32 | fmt.Println("Directory -", path) 33 | } else { 34 | fmt.Println("File -", path) 35 | } 36 | } 37 | return nil 38 | } 39 | -------------------------------------------------------------------------------- /Go_Basics/webRequest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saumya1singh/golang-notes/217fb634960c7e6c366cae4bbdf51067c960c924/Go_Basics/webRequest.png -------------------------------------------------------------------------------- /Go_Basics/writeToFile.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | content := "Hello from Go." 11 | /* create the File */ 12 | file, err := os.Create("./fromString.txt") 13 | checkError(err) 14 | defer file.Close() 15 | 16 | /* Write something to the file using WriteString() method 17 | it returns length of characters in the file */ 18 | length, err := io.WriteString(file, content) 19 | checkError(err) 20 | 21 | fmt.Printf("All done with file of %v charters\n", length) 22 | } 23 | 24 | func checkError(err error) { 25 | if err != nil { 26 | /* Panic is a built-in function that stops the ordinary flow of control and begins panicking. */ 27 | panic(err) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golang Notes 2 |
3 |
4 |