Skip to content

Commit

Permalink
docs: improve go code formatting and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradumnasaraf committed Feb 2, 2024
1 parent 0f11140 commit e1662f7
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Go/Concepts/12) slices2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func main() {

fmt.Println(cars)
var index int = 2
cars = append(cars[:index], cars[index+1:]...) // remove the element at index 2. :index is from 0 to index-1, [index+1:] is from index+1 to end
cars = append(cars[:index], cars[index+1:]...) // remove the element at index 2. :index is from 0 to index-1, [index+1:] is from index+1 to end. We can append both elements as well as a slice. ... is to denote a slice
fmt.Println(cars)

}
2 changes: 0 additions & 2 deletions Go/Concepts/15) if-else.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func main() {
fmt.Println("Your number is less than 10")
}

// Creating a variable inside the if statement

if 9%2 == 0 {
fmt.Println("9 is even")
} else {
Expand Down
7 changes: 5 additions & 2 deletions Go/Concepts/16) loops.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {
// }

sum := 0
for i := 0; i < 10; i++ {
for i := 0; i < 10; i++ { //for [initialization]; [condition]; [post] { }
sum += i
}
fmt.Println(sum)
Expand All @@ -22,16 +22,19 @@ func main() {
fmt.Println(days[i])
}

// for [index] := range [array] { }
for i := range days { // Better way to iterate over an array
fmt.Println(days[i])
}

// for [index], [value] := range [array] { }
for i, day := range days { // This is the preferred way to iterate over an array
fmt.Println(i, day) // i is the index and day is the value
}

// for _, [value] := range [array] { }
for _, day := range days { // we can ignore the index by using _
fmt.Println(day)
fmt.Println(day)
}

}
2 changes: 2 additions & 0 deletions Go/Concepts/18) break-continue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
)

// continue: Will skip that ireration, but will not break the look unlike "break"

func main() {
for i := 0; i < 5; i++ {
if i == 3 { // Here 3 will not be printed
Expand Down
2 changes: 1 addition & 1 deletion Go/Concepts/19) function.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func greetUser() {
fmt.Println("Hello, welcome to the app")
}

func addTwoNumbers(a int, b int) int { // Function with Para
func addTwoNumbers(a int, b int) int { // Function with Para and retun value
return a + b
}

Expand Down
19 changes: 9 additions & 10 deletions Go/Concepts/20) methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ package main

import "fmt"


type UserData struct { // struct is a collection of fields.
fistName string
lastName string
email string
type UserData struct { // struct is a collection of fields.
fistName string
lastName string
email string
numberOfTickets int
}

func main() {
user := UserData{ // struct initialization
fistName: "John",
lastName: "Doe",
email: "[email protected]",
fistName: "John",
lastName: "Doe",
email: "[email protected]",
numberOfTickets: 2,
}

fmt.Println(user)

user.greetUser() // Method call
user.greetUser() // Method call
}

func (u UserData) greetUser() { // method with receiver
fmt.Println("Hello", u.fistName, u.lastName)
}
}
23 changes: 11 additions & 12 deletions Go/Concepts/23) files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
)

func main() {
content := "Hello, playground!"

file, err := os.Create("./test.txt")
// Process to create, write and read a file

content := "Hey I am Pradumna"

//"." means current directory
file, err := os.Create("./hello.txt")

if err != nil {
panic(err)
Expand All @@ -21,20 +24,16 @@ func main() {
if err != nil {
panic(err)
}
fmt.Println("Length of the file is: ", length)
defer file.Close()

readFile("./test.txt")

}

func readFile(filename string){
fmt.Println("Length:", length)

databyte, err := ioutil.ReadFile(filename)
data, err := os.ReadFile("./hello.txt")

if err != nil {
panic(err)

}

fmt.Println("Text data inside a file is \n", string(databyte))
// data is of type []byte so we need to convert it to string
fmt.Println(string(data))
}
4 changes: 2 additions & 2 deletions Go/Concepts/24) handling-web-req.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand All @@ -16,7 +16,7 @@ func main() {
fmt.Printf("Response is of type: %T\n", res)
fmt.Println(res.Status)

databytes, err := ioutil.ReadAll(res.Body) //We can't read the response directly. So we use ioutil.ReadAll()
databytes, err := io.ReadAll(res.Body) //We can't read the response directly. So we use ioutil.ReadAll()
checkNilError(err)

content := string(databytes) // convert the byte array to string
Expand Down
23 changes: 11 additions & 12 deletions Go/Concepts/25) handling-url.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@ const myURL = "https://pradumnasaraf.dev:3000/services?service=twitter"

func main() {

fmt.Println(myURL)

// Parsing

// Parsing the URL
results, _ := url.Parse(myURL)
fmt.Println(results.Scheme)
fmt.Println(results.Host)
fmt.Println(results.Port())
fmt.Println(results.RawQuery)
fmt.Println(results.Query())
fmt.Println(results.Scheme) // Scheme is the protocol used. Eg: http, https, ftp, etc
fmt.Println(results.Host) // Host is the domain name
fmt.Println(results.Port()) // Port is the port number
fmt.Println(results.RawQuery) // RawQuery is the query string
fmt.Println(results.Query()) // Query is the query string in map form

data := results.Query()
fmt.Println(data["service"])

// &url.URL{} is a pointer to a url.URL type
partsOfUrl := &url.URL{
Scheme: "https",
Host: "pradumnasaraf.dev",
Path: "/services",
Scheme: "https",
Host: "pradumnasaraf.dev",
Path: "/services",
RawQuery: "user=pradumna",
}

// String() method is used to convert the url.URL type to string
anotherUrl := partsOfUrl.String()
fmt.Println(anotherUrl)
}
2 changes: 1 addition & 1 deletion Go/Concepts/26) basic-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ func main() {
http.ListenAndServe(":8080", nil)
}

func hello(w http.ResponseWriter, r *http.Request) {
func hello(w http.ResponseWriter, r *http.Request) { // w is used to write the response and r is used to read the request
w.Write([]byte("Hello World"))
}

0 comments on commit e1662f7

Please sign in to comment.