├── LICENSE ├── README.md ├── addinput.go ├── array.go ├── average.go ├── boolean-and-condition.go ├── calculator.go ├── countwords.go ├── crypto.go ├── findip.go ├── firstandlast.go ├── guessnumber.go ├── hello-world.go ├── itemexists.go ├── largestnumber.go ├── leap_year.go ├── loops.go ├── maths.go ├── nameserver.go ├── oddoreven.go ├── permutations.go ├── readdirectory.go ├── repeat.go ├── rest-api.go ├── slice.go ├── sort.go ├── switch.go ├── time.go ├── variables.go ├── vartype.go ├── vowelcheck.go └── webserver.go /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Michael Okoh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GO LANG EXAMPLES 2 | 3 | Various GO Lang examples to help you get a better understanding of the langauage 4 | 5 | To test any of the examples: 6 | 7 | go run filename.go 8 | 9 | ## Contributors 10 | Adebambo Oyelaja -------------------------------------------------------------------------------- /addinput.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | firstNumber, secondNumber := getUserInput() 9 | sum := addTwoNumbers(firstNumber, secondNumber) 10 | fmt.Println(firstNumber, "+", secondNumber, "=", sum) 11 | } 12 | 13 | func addTwoNumbers(firstNumber, secondNumber int) int { 14 | return firstNumber + secondNumber 15 | } 16 | 17 | func getUserInput() (int, int) { 18 | var firstNumber int 19 | var secondNumber int 20 | 21 | fmt.Println("Enter first number: ") 22 | fmt.Scan(&firstNumber) 23 | fmt.Println("Enter second number: ") 24 | fmt.Scan(&secondNumber) 25 | 26 | return firstNumber, secondNumber 27 | } 28 | -------------------------------------------------------------------------------- /array.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // Declare an array to hold 5 integers 7 | numbers := [5]int{1, 2, 3, 4, 5} 8 | 9 | // Print a single element 10 | fmt.Println(numbers[3]) 11 | 12 | // Loop through the array to print every value 13 | for _, number := range numbers { 14 | fmt.Println(number) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /average.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | //calulate an average of a range of numbers entered 6 | func main() { 7 | var num [100]int 8 | var temp, sum, avg int 9 | // chose number of elemets to work with 10 | fmt.Print("Enter number of elements: ") 11 | fmt.Scanln(&temp) 12 | for i := 0; i < temp; i++ { 13 | //enter numbers to calculate avergae from 14 | fmt.Print("Enter the number : ") 15 | fmt.Scanln(&num[i]) 16 | sum += num[i] 17 | } 18 | 19 | avg = sum / temp 20 | fmt.Printf("The Average of entered %d number(s) is %d", temp, avg) 21 | } 22 | -------------------------------------------------------------------------------- /boolean-and-condition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | subject := "Name" //Change name to your name of choice 7 | single := true 8 | 9 | if single { 10 | fmt.Println(subject, "is Single") 11 | } else { 12 | fmt.Println(subject, "is not single") 13 | } 14 | 15 | employed := false 16 | 17 | if employed { 18 | fmt.Println(subject, " is Employed") 19 | } else { 20 | fmt.Println(subject, "is Unemployed and Jobless at the same time") 21 | } 22 | 23 | // and statement 24 | if employed && single { 25 | fmt.Println(subject, "is single and employed") 26 | } else { 27 | fmt.Println(subject, "is single and jobless") 28 | } 29 | 30 | // or statement 31 | if employed || single { 32 | fmt.Println(subject, "is single or employed") 33 | } else { 34 | fmt.Println(subject, "is not single and is jobless") 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /calculator.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var choice int 7 | var firstNumber float32 8 | var secondNumber float32 9 | 10 | fmt.Println("Select an Operation") 11 | fmt.Println("1. Add") 12 | fmt.Println("2. Subtract") 13 | fmt.Println("3. Multiply") 14 | fmt.Println("4. Divide") 15 | fmt.Println("Enter choice(1/2/3/4): ") 16 | fmt.Scan(&choice) 17 | fmt.Println("Enter first number: ") 18 | fmt.Scan(&firstNumber) 19 | fmt.Println("Enter second number: ") 20 | fmt.Scan(&secondNumber) 21 | 22 | if choice == 1 { 23 | fmt.Println(firstNumber, "+", secondNumber, "=", firstNumber+secondNumber) 24 | } else if choice == 2 { 25 | fmt.Println(firstNumber, "-", secondNumber, "=", firstNumber-secondNumber) 26 | } else if choice == 3 { 27 | fmt.Println(firstNumber, "*", secondNumber, "=", firstNumber*secondNumber) 28 | } else if choice == 4 { 29 | fmt.Println(firstNumber, "/", secondNumber, "=", firstNumber/secondNumber) 30 | } else { 31 | fmt.Println("Invalid operation selected") 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /countwords.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | //count appreance of words in a string 9 | func wordCount(str string) map[string]int { 10 | wordList := strings.Fields(str) 11 | counts := make(map[string]int) 12 | for _, word := range wordList { 13 | _, ok := counts[word] 14 | if ok { 15 | counts[word]++ 16 | } else { 17 | counts[word] = 1 18 | } 19 | } 20 | return counts 21 | } 22 | 23 | func main() { 24 | strLine := "I love writing code, I used to work with PHP but now I am loving GO more! Do you code in GO" 25 | for index, element := range wordCount(strLine) { 26 | fmt.Println(index, "=>", element) 27 | } 28 | fmt.Println("There are", len(strLine), "characters in the defined string.") //count lenght of string 29 | } 30 | -------------------------------------------------------------------------------- /crypto.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "crypto/hmac" 5 | "crypto/md5" 6 | "crypto/sha256" 7 | "crypto/sha512" 8 | "encoding/base64" 9 | "fmt" 10 | "io" 11 | ) 12 | 13 | func main() { 14 | fmt.Println("Enter a word to hash:") 15 | var input string 16 | fmt.Scanln(&input) 17 | 18 | md5 := md5.New() 19 | sha_256 := sha256.New() 20 | sha_512 := sha512.New() 21 | io.WriteString(md5, input) 22 | sha_256.Write([]byte(input)) 23 | sha_512.Write([]byte(input)) 24 | sha_512_256 := sha512.Sum512_256([]byte(input)) 25 | hmac512 := hmac.New(sha512.New, []byte("secret")) 26 | hmac512.Write([]byte(input)) 27 | 28 | //4db45e622c0ae3157bdcb53e436c96c5 29 | fmt.Printf("md5:\t\t%x\n", md5.Sum(nil)) 30 | 31 | //eb7a03c377c28da97ae97884582e6bd07fa44724af99798b42593355e39f82cb 32 | fmt.Printf("sha256:\t\t%x\n", sha_256.Sum(nil)) 33 | 34 | //5cdaf0d2f162f55ccc04a8639ee490c94f2faeab3ba57d3c50d41930a67b5fa6915a73d6c78048729772390136efed25b11858e7fc0eed1aa7a464163bd44b1c 35 | fmt.Printf("sha512:\t\t%x\n", sha_512.Sum(nil)) 36 | 37 | //34c614af69a2550a4d39138c3756e2cc50b4e5495af3657e5b726c2ac12d5e60 38 | fmt.Printf("sha512_256:\t%x\n", sha_512_256) 39 | 40 | //GBZ7aqtVzXGdRfdXLHkb0ySp/f+vV9Zo099N+aSv+tTagUWuHrPeECDfUyd5WCoHBe7xkw2EdpyLWx+Ge4JQKg== 41 | fmt.Printf("hmac512:\t%s\n", base64.StdEncoding.EncodeToString(hmac512.Sum(nil))) 42 | } 43 | -------------------------------------------------------------------------------- /findip.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "net/http" 7 | ) 8 | 9 | //find ip address using ifify.org 10 | func main() { 11 | url := "https://api.ipify.org?format=text" 12 | fmt.Printf("Please wait while I fetch your IP address from ipify\n") 13 | resp, err := http.Get(url) 14 | if err != nil { 15 | panic(err) 16 | } 17 | defer resp.Body.Close() 18 | ip, err := ioutil.ReadAll(resp.Body) 19 | if err != nil { 20 | panic(err) 21 | } 22 | fmt.Printf("My IP is:%s\n", ip) 23 | } 24 | -------------------------------------------------------------------------------- /firstandlast.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | //get first and last element of a slice and remove last 6 | func main() { 7 | intSlice := []int{1, 2, 3, 4, 5} 8 | fmt.Printf("Slice: %v\n", intSlice) 9 | 10 | last := intSlice[len(intSlice)-1] 11 | fmt.Printf("Last element: %v\n", last) 12 | 13 | first := intSlice[:1] 14 | fmt.Printf("First element: %d\n", first) 15 | 16 | remove := intSlice[:len(intSlice)-1] 17 | fmt.Printf("Remove Last: %v\n", remove) 18 | } 19 | -------------------------------------------------------------------------------- /guessnumber.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | var guess int 10 | 11 | var trialcount int = 1 12 | 13 | func main() { 14 | 15 | source := rand.NewSource(time.Now().UnixNano()) 16 | randomizer := rand.New(source) 17 | secretNumber := randomizer.Intn(10) 18 | 19 | 20 | fmt.Println("Guess the number") 21 | 22 | for{ 23 | fmt.Println("Please input your guess") 24 | fmt.Scan(&guess) 25 | if trialcount > 5{ 26 | fmt.Println("You lose") 27 | break 28 | }else{ 29 | if guess > secretNumber { 30 | fmt.Println("Too Big") 31 | } else if guess < secretNumber { 32 | fmt.Println("Too small") 33 | } else { 34 | fmt.Println("You win!") 35 | break 36 | } 37 | } 38 | trialcount ++ 39 | } 40 | } -------------------------------------------------------------------------------- /hello-world.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | //print hello world using fmt.Println 6 | func main() { 7 | helloWorld() 8 | } 9 | 10 | func helloWorld() { 11 | fmt.Println("Hellow World 🤤") 12 | } 13 | -------------------------------------------------------------------------------- /itemexists.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | ) 7 | 8 | //check if any item exists or not 9 | func main() { 10 | var strSlice = []string{"banana", "apple", "peach", "managa", "dates"} 11 | fmt.Println(itemExists(strSlice, "apple")) 12 | fmt.Println(itemExists(strSlice, "orange")) 13 | } 14 | 15 | func itemExists(slice interface{}, item interface{}) bool { 16 | s := reflect.ValueOf(slice) 17 | 18 | if s.Kind() != reflect.Slice { 19 | panic("Invalid data-type") 20 | } 21 | 22 | for i := 0; i < s.Len(); i++ { 23 | if s.Index(i).Interface() == item { 24 | return true //returned because apple is part of strSlice 25 | } 26 | } 27 | 28 | return false //returned because orange is missing from strSlice 29 | } 30 | -------------------------------------------------------------------------------- /largestnumber.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | //find largest of three numbers 6 | func main() { 7 | fmt.Println("Enter 3 numbers :") 8 | var first int 9 | fmt.Scanln(&first) 10 | var second int 11 | fmt.Scanln(&second) 12 | var third int 13 | fmt.Scanln(&third) 14 | /* check the boolean condition using if statement */ 15 | if first >= second && first >= third { 16 | fmt.Println(first, "is Largest among three numbers.") /* if condition is true then print the following */ 17 | } 18 | if second >= first && second >= third { 19 | fmt.Println(second, "is Largest among three numbers.") 20 | } 21 | if third >= first && third >= second { 22 | fmt.Println(third, "is Largest among three numbers") 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /leap_year.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | //check leap year 6 | func checkLeapYear() { 7 | fmt.Print("Enter a year to check: ") 8 | var year int 9 | fmt.Scanln(&year) 10 | if year%4 == 0 { 11 | if year%100 == 0 { 12 | if year%400 == 0 { 13 | fmt.Println("The year", year, "is a leap year") 14 | } else { 15 | fmt.Println("The year", year, "is not a leap year") 16 | } 17 | } else { 18 | fmt.Println("The year", year, "is a leap year") 19 | } 20 | } else { 21 | fmt.Println("The year", year, "is a not a leap year") 22 | } 23 | } 24 | 25 | func main() { 26 | checkLeapYear() 27 | } 28 | -------------------------------------------------------------------------------- /loops.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | //loop numbers lesser than 10 6 | func main() { 7 | // Counting Loop 8 | i := 0 9 | 10 | for i < 10 { 11 | fmt.Println(i) 12 | i = i + 1 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /maths.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | ) 7 | 8 | func main() { 9 | mathmethods() 10 | fahrenheit() 11 | feetconversion() 12 | } 13 | 14 | func mathmethods() { 15 | a := math.Min(100, 20) 16 | fmt.Println("Minimum number between 100 and 20 is", a) 17 | 18 | b := math.Max(100, 20) 19 | fmt.Println("Maximum number between 100 and 20 is", b) 20 | 21 | num1 := 10 22 | num2 := 2 23 | fmt.Println("The square root of 16 is", math.Sqrt(16)) //square root 24 | fmt.Println(num1, "+", num2, "=", num1+num2) // Addition 25 | fmt.Println(num1, "*", num2, "=", num1*num2) // Multiplication 26 | fmt.Println(num1, "-", num2, "=", num1-num2) // Subtraction 27 | fmt.Println(num1, "/", num2, "=", num1/num2) // Division 28 | fmt.Println(num1, "%", num2, "=", num1%num2) // Remainder 29 | 30 | u := (10 + 10) * 5 31 | fmt.Println("10 + 10 * 5 = ", u) 32 | } 33 | 34 | // feet to metre conversion 35 | func feetconversion() { 36 | // define feet for use as feet to convert to metres 37 | var feet float32 38 | fmt.Println("Enter feet length to convert") 39 | fmt.Scan(&feet) 40 | var feetvalue float32 41 | feetvalue = 3.28 42 | result := (feet / feetvalue) 43 | // fmt.Println(feet, "feet is", int(result), "metres") 44 | fmt.Println(feet, "feet is", result, "metres") 45 | } 46 | 47 | // farenheit to celcius conversion 48 | func fahrenheit() { 49 | // define farenheit for use as the degress in farenheit to convert 50 | var fahrenheit int 51 | // ask for the farenheit value to be converted 52 | fmt.Println("Enter farenheit degrees to convert") 53 | // scan the inputed integer 54 | fmt.Scan(&fahrenheit) 55 | // perform farenheit to celcius calculation 56 | celcius := (fahrenheit - 32) * 5 / 9 57 | // print the result 58 | fmt.Println(fahrenheit, "in celcius is", celcius, "degree celcius") 59 | } 60 | -------------------------------------------------------------------------------- /nameserver.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | ) 7 | 8 | //look up name servers of any domain 9 | func main() { 10 | nameserver, _ := net.LookupNS("google.com") 11 | for _, ns := range nameserver { 12 | fmt.Println(ns) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /oddoreven.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | checkNumber() 7 | } 8 | 9 | //check odd or even number 10 | func checkNumber() { 11 | fmt.Print("Enter number : ") 12 | var n int 13 | fmt.Scanln(&n) 14 | if n%2 == 0 { 15 | fmt.Println(n, "is an Even number") 16 | } else { 17 | fmt.Println(n, "is an Odd number") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /permutations.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "strconv" 7 | ) 8 | 9 | func rangeSlice(start, stop int) []int { 10 | if start > stop { 11 | panic("Slice ends before it started") 12 | } 13 | xs := make([]int, stop-start) 14 | for i := 0; i < len(xs); i++ { 15 | xs[i] = i + 1 + start 16 | } 17 | return xs 18 | } 19 | 20 | func permutation(xs []int) (permuts [][]int) { 21 | var rc func([]int, int) 22 | rc = func(a []int, k int) { 23 | if k == len(a) { 24 | permuts = append(permuts, append([]int{}, a...)) 25 | } else { 26 | for i := k; i < len(xs); i++ { 27 | a[k], a[i] = a[i], a[k] 28 | rc(a, k+1) 29 | a[k], a[i] = a[i], a[k] 30 | } 31 | } 32 | } 33 | rc(xs, 0) 34 | 35 | return permuts 36 | } 37 | 38 | func main() { 39 | if len(os.Args) < 2 { 40 | fmt.Println("Error: Add 1 numeric value ex. go run permutation.go 2") 41 | os.Exit(1) 42 | } 43 | num, err := strconv.Atoi(os.Args[1]) 44 | if err != nil { 45 | fmt.Println(err.Error()) 46 | os.Exit(1) 47 | } 48 | 49 | noOfPerms := permutation(rangeSlice(0, num)) 50 | fmt.Println("Number of Permutation:", len(noOfPerms)) 51 | for i := 0; i < len(noOfPerms); i++ { 52 | fmt.Println(noOfPerms[i]) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /readdirectory.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | ) 8 | 9 | //read directory and print info 10 | func readCurrentDir() { 11 | file, err := os.Open(".") 12 | if err != nil { 13 | log.Fatalf("failed opening directory: %s", err) 14 | } 15 | defer file.Close() 16 | 17 | fileList, _ := file.Readdir(0) 18 | 19 | fmt.Printf("\nName\t\tSize\tIsDirectory Last Modification\n") 20 | 21 | for _, files := range fileList { 22 | fmt.Printf("\nName: %-15s Size: %-7v Directory?: %-12v Modified: %v\n", files.Name(), files.Size(), files.IsDir(), files.ModTime()) 23 | } 24 | } 25 | 26 | func main() { 27 | readCurrentDir() 28 | } 29 | -------------------------------------------------------------------------------- /repeat.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | repeat("Github", 5) 7 | } 8 | 9 | func repeat(word string, reps int) { 10 | for i := 0; i < reps; i++ { 11 | fmt.Print(word) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /rest-api.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "log" 6 | "net/http" 7 | 8 | "github.com/gorilla/mux" 9 | ) 10 | 11 | // Application Object 12 | type Application struct { 13 | ID string `json:"id,omitempty"` 14 | Name string `json:"name,omitempty"` 15 | Description string `json:"description,omitempty"` 16 | Developer string `json:"developer,omitempty"` 17 | Details *Details `json:"details,omitempty"` 18 | } 19 | 20 | // Address Object 21 | type Details struct { 22 | Language string `json:"language,omitempty"` 23 | Type string `json:"type,omitempty"` 24 | } 25 | 26 | // Define People 27 | var app []Application 28 | 29 | // Define port to run 30 | const ( 31 | PORT = ":1909" 32 | ) 33 | 34 | // GetAppEndPoint A SINGLE APP USING ID 35 | func GetAppEndpoint(w http.ResponseWriter, req *http.Request) { 36 | params := mux.Vars(req) 37 | for _, item := range app { 38 | if item.ID == params["id"] { 39 | json.NewEncoder(w).Encode(item) 40 | return 41 | } 42 | } 43 | json.NewEncoder(w).Encode(&Application{}) 44 | } 45 | 46 | // GetAppsEndpoint GET ALL APPS 47 | func GetAppsEndpoint(w http.ResponseWriter, req *http.Request) { 48 | json.NewEncoder(w).Encode(app) 49 | } 50 | 51 | // CreateAppEndpoint CREATE A APP 52 | func CreateAppEndpoint(w http.ResponseWriter, req *http.Request) { 53 | var appnew Application 54 | _ = json.NewDecoder(req.Body).Decode(&appnew) 55 | app = append(app, appnew) 56 | json.NewEncoder(w).Encode(app) 57 | } 58 | 59 | // DeleteAppEndpoint DELETE AN APP 60 | func DeleteAppEndpoint(w http.ResponseWriter, req *http.Request) { 61 | params := mux.Vars(req) 62 | for index, item := range app { 63 | if item.ID == params["id"] { 64 | app = append(app[:index], app[index+1:]...) 65 | break 66 | } 67 | } 68 | json.NewEncoder(w).Encode(app) 69 | } 70 | 71 | // ROUTING 72 | func serveRequests() { 73 | router := mux.NewRouter().StrictSlash(true) 74 | router.HandleFunc("/app", GetAppsEndpoint).Methods("GET") 75 | router.HandleFunc("/app/{id}", GetAppEndpoint).Methods("GET") 76 | router.HandleFunc("/app", CreateAppEndpoint).Methods("POST") 77 | router.HandleFunc("/app/{id}", DeleteAppEndpoint).Methods("DELETE") 78 | log.Fatal(http.ListenAndServe(PORT, router)) 79 | 80 | } 81 | 82 | func main() { 83 | app = append(app, Application{ID: "1", Name: "go-api", Description: "Playing around with GO CRUD operations", Developer: "ozombo", Details: &Details{Language: "GO", Type: "Backend/API"}}) 84 | app = append(app, Application{ID: "2", Name: "go-cli", Description: "Cli operations in GO"}) 85 | log.Println("Sample apps populated, your api is serving at: http://127.0.0.1", PORT, "/") 86 | serveRequests() 87 | } 88 | -------------------------------------------------------------------------------- /slice.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | // define slice1 as four numbers 9 | slice1 := []int{1, 2, 3, 4} 10 | // join slice1 into slice2 11 | slice2 := append(slice1, 4, 5, 6) 12 | fmt.Println("Adding slice1 into slice2 to give", slice2) 13 | 14 | // define slice 3 as three numbers 15 | slice3 := []int{1, 2, 3} 16 | // make slice4 as two numbers 17 | slice4 := make([]int, 2) 18 | // copy slice3 into slice4 19 | copy(slice4, slice3) 20 | fmt.Println(slice3, slice4) 21 | } 22 | -------------------------------------------------------------------------------- /sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | //sorts alphabets alphabetically and string incrementally 9 | func main() { 10 | alph := []string{"z", "v", "a", "d", "g", "j"} 11 | sort.Strings(alph) 12 | fmt.Println("Alphabets:", alph) 13 | 14 | integer := []int{71, 902, 43} 15 | sort.Ints(integer) 16 | fmt.Println("Integer: ", integer) 17 | 18 | s := sort.IntsAreSorted(integer) 19 | fmt.Println("Sorted?: ", s) 20 | } 21 | -------------------------------------------------------------------------------- /switch.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // Declare the desire 7 | desire := "money" 8 | 9 | switch desire { 10 | case "love": 11 | fmt.Println("❤️❤️❤️❤️❤️❤️❤️") 12 | 13 | case "money": 14 | fmt.Println("💰💰💰💰💰💰💰") 15 | 16 | default: 17 | fmt.Println("I cannot give you what you desire") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /time.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "time" 7 | ) 8 | 9 | func main() { 10 | fmt.Println(strconv.FormatInt(time.Now().Unix(), 10)) 11 | //1575476458 12 | fmt.Println(time.Now().Format(time.RFC850)) 13 | //Friday, 30-Sep-19 00:51:14 CEST 14 | fmt.Println(time.Now().Format(time.RFC1123Z)) 15 | //Fri, 30 Sep 2019 00:51:14 +0200 16 | fmt.Println(time.Now().Format("20060102150405")) 17 | //20191204171945 18 | fmt.Println(time.Unix(1401403874, 0).Format("02.01.2006 15:04:05")) 19 | //30.05.2019 00:51:14 20 | fmt.Println(time.Unix(123456789, 0).Format(time.RFC3339)) 21 | //1973-11-29T22:33:09+01:00 22 | fmt.Println(time.Unix(1234567890, 0).Format(time.RFC822)) 23 | //14 Feb 09 00:31 CET 24 | } 25 | -------------------------------------------------------------------------------- /variables.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main () { 6 | name := "Trojan Okoh" 7 | age := 100 8 | height := 5.85 9 | 10 | fmt.Println("My name is ", name, "\nI am ", age, " years old and", height, " feets tall") 11 | } 12 | -------------------------------------------------------------------------------- /vartype.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | // determine type of variable 8 | func main() { 9 | msg := "Hello" 10 | fmt.Printf("%v is %T\n", msg, msg) 11 | num := 32 12 | fmt.Printf("%v is %T\n", num, num) 13 | val := 1 == 1 //check boolean 14 | fmt.Printf("Result is is %T\n", val) 15 | } 16 | -------------------------------------------------------------------------------- /vowelcheck.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | func main() { 9 | names() 10 | } 11 | 12 | func names() { 13 | fmt.Println("Enter your name:") 14 | 15 | var name string 16 | fmt.Scanln(&name) 17 | // Check whether name has a vowel 18 | for _, v := range strings.ToLower(name) { 19 | if v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u' { 20 | fmt.Println("Your name contains a vowel.") 21 | return 22 | } 23 | } 24 | fmt.Println("Your name does not contain a vowel.") 25 | } -------------------------------------------------------------------------------- /webserver.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | ) 7 | 8 | func main() { 9 | fmt.Println("Server up and running at :8080. Visit http://localhost:8080/yourname to test") 10 | http.HandleFunc("/", HelloServer) 11 | http.ListenAndServe(":8080", nil) 12 | } 13 | 14 | // greet user echoing the text after the trailing slash eg: http://localhost:8080/username will say Hello, username! 15 | func HelloServer(w http.ResponseWriter, r *http.Request) { 16 | fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) 17 | } 18 | --------------------------------------------------------------------------------