Skip to content

grimcoder/golang_studies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Certainly! More examples will help you grasp the key concepts. Here's the expanded learning plan with examples.

1. Installing Go and Setting Up Your Environment

Resources

Example: Verify Installation

go version

2. Basic Language Syntax and Data Structures

2.1 Variables and Data Types

Declare different types of variables.

var x int = 10
y := 20
var z float64 = 3.14

2.2 Control Structures

Examples of if, for, and switch.

// If-Else
if x > y {
    fmt.Println("x is greater")
} else {
    fmt.Println("y is greater")
}

// For loop
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// Switch
switch x {
case 10:
    fmt.Println("Ten")
default:
    fmt.Println("Not Ten")
}

2.3 Functions

Declaring and calling a function.

func add(a int, b int) int {
    return a + b
}

2.4 Data Structures

Arrays, slices, and maps.

// Array
var arr [5]int = [5]int{1, 2, 3, 4, 5}

// Slice
slice := []int{1, 2, 3}

// Map
m := map[string]int{"Apple": 1, "Orange": 2}

Resources


3. Functions and Packages

3.1 Function Syntax

func greet(name string) string {
    return "Hello " + name
}

3.2 Packages and Imports

Example of importing a custom package.

Create a file math.go:

package math

func Add(a int, b int) int {
    return a + b
}

Import and use it:

import "path/to/math"

result := math.Add(1, 2)

Resources


4. Web Handling with HTTP

4.1 HTTP Handlers

Simple HTTP server with a handler.

func helloWorld(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, world!")
}

4.2 Routing with Gorilla Mux

Installation and basic routing example.

go get -u github.com/gorilla/mux

Example:

r := mux.NewRouter()
r.HandleFunc("/", helloWorld)

Resources


5. Building REST APIs

5.1 GET and POST Methods

How to handle GET and POST requests.

r.HandleFunc("/users", getUsers).Methods("GET")
r.HandleFunc("/users", createUser).Methods("POST")

Resources


6. Databases

6.1 PostgreSQL Integration

Install PostgreSQL driver and connect.

go get -u github.com/lib/pq
connStr := "user=username dbname=mydb sslmode=disable"
db, err := sql.Open("postgres", connStr)

Resources


This is quite a lot to cover in one day, but it should give you a strong foundation in Go backend development. I've included resources to delve deeper into each topic.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages