Wednesday, 12 March 2025

curl check apache/server version

 curl --HEAD https://mysite.com/

HTTP/1.1 200 OK

Date: Wed, 12 Mar 2025 18:09:35 GMT

Server: Apache/2.4.63 (Unix) OpenSSL/3.0.15

Last-Modified: Wed, 08 Jan 2025 21:56:34 GMT

ETag: "24d-62b38ef635542"

Accept-Ranges: bytes

Content-Length: 589

Content-Type: text/html


GO LANG get working dir

// This is important for docker container , cause ur path might be different

 f, _ := os.Getwd()  

fmt.Println(filepath.Base(f))

fmt.Println(filepath.Dir(f))

Wednesday, 5 March 2025

GO LANG combine duplicates using MAP

 package main


import (

"fmt"

)


type Item struct {

Name  string

Value int

}


func combineDuplicates(items []Item) []Item {

// Use a map with string keys (Name)

itemMap := make(map[string]Item)


for _, item := range items {

if existing, found := itemMap[item.Name]; found {

// Merge logic: sum values

existing.Value += item.Value

itemMap[item.Name] = existing

} else {

// Add new unique item

itemMap[item.Name] = item

}

}


// Convert map back to slice

result := make([]Item, 0, len(itemMap))

for _, item := range itemMap {

result = append(result, item)

}


return result

}


func main() {

items := []Item{

{Name: "A", Value: 10},

{Name: "B", Value: 20},

{Name: "A", Value: 15},

{Name: "C", Value: 30},

{Name: "B", Value: 25},

}


combinedItems := combineDuplicates(items)

fmt.Println("Combined Items:")

for _, item := range combinedItems {

fmt.Printf("Name: %s, Value: %d\n", item.Name, item.Value)

}

}



// Reassigning 

Why is reassignment necessary?

  • In Go, when you access a struct value from a map like this:

existing := itemMap[item.Name]

  • You are getting a copy of the struct, not a reference.

  • If you modify existing.Value, it only affects the local copy, not the original struct in the map.

  • To actually update the map entry, you need to explicitly reassign the modified struct back:


  • itemMap[item.Name] = existing

  • Friday, 14 February 2025

    Quick Docker commands

     # list of containers

    docker ps

    # stop container

    docker stop <container id>

    # list of images

    docker image ls 

    # remove images

    docker rm <image id>


    # add to docker images 

    docker load < image_latest.tar.gz

    Thursday, 13 February 2025

    GO LANG Pointer, derefernece, nil

    https://forum.golangbridge.org/t/dereferencing-pointer/21201/4

    //Manual derefenrece only need for map

    func myProcess(a *map[string]string) { t := (*a)["whatever"] ... }

    // GO auto deference if struct is pointer :

    Yes, but don’t write it in that complicated way. In Go, we value readability and simplicity. Simply write:

    func happyBirthday(p *person) { p.age++ }


    --------------------------------------------------------------------------------------------------------

    receiver , receiver with pointer


    https://go.dev/tour/methods/4


    package main


    import (

    "fmt"

    "math"

    )


    type Vertex struct {

    X, Y float64

    }


    func (v Vertex) Abs() float64 {

    return math.Sqrt(v.X*v.X + v.Y*v.Y)

    }


    func (v *Vertex) Scale(f float64) {

    v.X = v.X * f

    v.Y = v.Y * f

    }


    func main() {

    v := Vertex{3, 4}

          //result is 5

    fmt.Println(v.Abs())

    v.Scale(10)

    //result is 50

    fmt.Println(v.Abs())

    }


    # reciever with pointer changes the address of original value due to auto dereferncing




    -------------------------------







    https://medium.com/@jamal.kaksouri/a-comprehensive-guide-to-pointers-in-go-4acc58eb1f4d

    var x int = 10
    var ptr *int = &x

    fmt.Println(x) // output: 10
    fmt.Println(ptr) // output: 0xc0000140a8
    fmt.Println(*ptr) // output: 10


    ---------------------


    var ptr *int = new(int)

    fmt.Println(ptr) // output: 0xc0000160c0
    fmt.Println(*ptr) // output: 0

    *ptr = 10
    fmt.Println(*ptr) // output: 10

    ptr = nil


    In this example,

    we declare a pointer variable ptr

    of type *int and use the new function to allocate memory for an integer value.

    We then print the memory address stored in ptr,

    which is the address of the newly allocated memory block.

    We also print the value of *ptr, which is the value stored at the memory address,

    which is initially set to 0.

    We then assign the value 10 to the memory location pointed to

    by ptr using the * operator. Finally, we set the pointer variable to nil,

    which frees the memory block allocated by new.


    -----------------------------------------------------------------------

    error will occur when derefencing nil pointer


    The error occurs if you try to dereference (*ptr) a nil pointer:


    https://stackoverflow.com/questions/59964619/difference-using-pointer-in-struct-fields
    use pointer in struct to allow nil
    allow pointer type *string to allow nil value

    *namePtr derference the pointet

    var namePtr *string // namePtr is nil fmt.Println(*namePtr) // ❌ ERROR: nil pointer dereference

    Check nil

    package main import "fmt" func main() { var namePtr *string // initialized pointer (nil) if namePtr == nil { fmt.Println("Pointer is nil, cannot dereference") } else { fmt.Println(*namePtr) // This won't execute// dereferencing pointer } }



    // Make a pointer
    namePtr := new(string)

    *namePtr = "Bob" // Assign value to the dereferenced pointer



    Using a Pointer for Nested Structs

    type User struct { Name string `json:"name"` Email string `json:"email"` Age int `json:"age"` Address *Address `json:"address,omitempty"` // Pointer to Address struct }
    • Address *Address means the Address field is a pointer to an Address struct, not the actual struct itself.
    • The * in the struct definition does not dereference the pointer—it just defines the type as a pointer.

    If Address is optional, use a pointer (*Address):

    go
    type User struct { Name string `json:"name"` Email string `json:"email"` Age int `json:"age"` Address *Address `json:"address,omitempty"` // Omits field if nil }

    📌 Response when Address is nil

    json
    { "name": "John Doe", "email": "john@example.com", "age": 30 }

    Friday, 7 February 2025

    Angular deploy a secondary project

     permanet:

    In your seondaryproject :

    angular.json

    "architect": { "build": { "options": { "outputPath": "dist/your-app-name", "baseHref": "/demo/", "deployUrl": "/demo/" }


    • --base-href=/demo/: Specifies the base URL for the application’s routing (i.e., how Angular handles navigation).
    • --deploy-url=/demo/: Specifies the URL path for static assets (JavaScript, CSS, images, fonts, etc.).

    Temporary(for ng build only)
    ng build --base-href=/demo/ --deploy-url=/demo/


    The configure web server to map a dir for this project

    Wednesday, 5 February 2025

    SQL With Recursive All, Union ALL


    https://www.techonthenet.com/mysql/union_all.php

    UNION ALL



    The MySQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It returns all rows from the query and it does not remove duplicate rows between the various SELECT statements.

    Each SELECT statement within the MySQL UNION ALL operator must have the same number of fields in the result sets with similar data types.


     https://stackoverflow.com/questions/18840998/recursive-in-sql

    The syntax that you are using looks like Postgres. "Recursion" in SQL is not really recursion, it is iteration. Your statement is:


    WITH RECURSIVE t(n) AS (

        SELECT 1

        UNION ALL

        SELECT n+1 FROM t WHERE n < 100

    )

    SELECT sum(n) FROM t;

    The statement for t is evaluated as:


    Evaluate the non-self-referring part (select  1).

    Then evaluate the self-referring part. (Initially this gives 2.)

    Then evaluation the self-referring part again. (3).

    And so on while the condition is still valid (n < 100).

    When this is done the t subquery is finished, and the final statement can be evaluated.



    The RECURSIVE from the query doesn't mean anything: it's just another name like n or t. What makes things recursive is that the CTE named t references itself inside the expression. To produce the result of the expression, the query engine must therefore recursively build the result, where each evaluation triggers the next. It reaches this point: SELECT n+1 FROM t... and has to stop and evaluate t. To do that, it has to call itself again, and so on, until the condition (n < 100) no longer holds. The SELECT 1 provides a starting point, and the WHERE n < 100 makes it so that the query does not recur forever.