Thursday, 14 March 2024

Go 2025 ! Go init && useful git libaries , pointers, lazy assignment, function returns, libraries, JWT web example 2024!!!!!!!!!2025!!!!!!!!!

2025~!!! Update

* go get vs go get -u

go get -u fetch package and update all depdenceis


GO WITH JWT example :

https://www.youtube.com/watch?v=ma7rUS_vW9M

* go get is use as package in go code

* go install is to use as cmd line


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

Go installation:

https://www.digitalocean.com/community/tutorials/how-to-install-go-on-ubuntu-20-04

replace version with latest version found :

https://go.dev/doc/install



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

to start getting libraries, go must init module:

https://go.dev/doc/tutorial/create-module


Create a greetings directory for your Go module source code.

For example, from your home directory use the following commands:


mkdir greetings

cd greetings

Start your module using the go mod init command.

Run the go mod init command, giving it your module path -- here, use example.com/greetings. If you publish a module, this must be a path from which your module can be downloaded by Go tools. That would be your code's repository.


For more on naming your module with a module path, see Managing dependencies.


$ go mod init example.com/greetings

go: creating new go.mod: module example.com/greetings

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

commonly used go libraries

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

decrypt -> save pwd to db

https://pkg.go.dev/golang.org/x/crypto#section-readme

go get -u golang.org/x/crypto/bycrypt

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

GORM  -> ORM (object relation model) easy mapping from DB to GO

https://gorm.io/docs/

go get -u gorm.io/gorm
go get -u gorm.io/driver/sqlite

GORM drivers:
https://gorm.io/docs/connecting_to_the_database.html
-------------------------------------------

GIN -> popular GO web framework

https://gin-gonic.com/docs/quickstart/

go get -u github.com/gin-gonic/gin


* GIN uses env varialbe PORT for all ports if not defined :

am going to add a new behaviour to Engine.Run().

  • Back-compatible: it will continue working as expected when an address is provided: router.Run(":3000")
  • NEW: no parameter: router.Run()
    Under this API, gin will try to read the PORT environment variable and use it.
    If the PORT variable is not defined, ":8080" would be used by default.
  • Under discussion: Support for App Engine
    can we detect in runtime that we are running under the app engine environment?
    if so, router.Run() should use the app engine's api.
https://github.com/joho/godotenv
https://stackoverflow.com/questions/74179363/how-to-modify-the-default-port-of-go-gin-my-8080-port-is-in-use

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

JWT-GO -> JWT token lbirary for GO

https://github.com/golang-jwt/jwt

go get -u github.com/golang-jwt/jwt/v5

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

GODOTENV -> GO environment virable

go get -u github.com/joho/godotenv

https://github.com/joho/godotenv

// Good errors package

Go errors ->

  go get -u github.com/pkg/errors

Go error groups -> 
     go get -u golang.org/x/sync/errgroup


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

ComplieDaemon -> watch for file change and complie on the fly

https://github.com/githubnemo/CompileDaemon

// Add to go.mod, packages fetched installed using go mod tidy then build
go get github.com/githubnemo/CompileDaemon
// does not update go.mode, installed locally should use this after go get because
// you want to use this package locally u get to watch for changes and compile
// before the go binary is built
go install github.com/githubnemo/CompileDaemon

CompileDaemon --command='./go-jwt'   (generated binary location and name)



Go Zap(logging)

https://github.com/uber-go/zap

go get -u go.uber.org/zap

Logg rolling (generate logs and roll)

go get -u gopkg.in/natefinch/lumberjack.v2

or 

import "gopkg.in/natefinch/lumberjack.v2"
go mod tidy

https://github.com/natefinch/lumberjack

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

Folder -> myClass.go

// Upper case allows this class to be used in other modules 

func LoadMyClass() {

}


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



Pointers to structs

Struct fields can be accessed through a struct pointer.

To access the field X of a struct when we have the struct pointer p we could write (*p).X. However, that notation is cumbersome, so the language permits us instead to write just p.X, without the explicit dereference.


package main


import "fmt"


type Vertex struct {

X int

Y int

}


func main() {

v := Vertex{1, 2}

p := &v

p.X = 1e9

fmt.Println(v)

}

---------------------------------
https://stackoverflow.com/questions/71865152/cannot-modify-object-by-reference-in-golang
package main

import "fmt"

type Animal struct {
    Name string
    Age  int
}

type Dog struct {
    Animal
    PropertyFoo1 int // some field specific to a dog
}

//Reference to the animal pointer in the place is is used
func (a *Animal) modifyAnimalName() {
    a.Name = "changed name!"
}

func main() {
    // create a new dog
    dog := Dog{
        Animal: Animal{
            Name: "Doggy",
            Age:  5,
        },
    }
    var animal *Animal = &dog.Animal
    fmt.Printf("name of dog before: %s \n", dog.Name)
    animal.modifyAnimalName()
    fmt.Printf("name of dog after: %s \n", dog.Name)
}

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

 detailed go lang pointer explanation :

https://www.youtube.com/watch?v=sTFJtxJXkaY


*int (pointer type)

*p(derefernecing pointer)


i := 3

p = &i

*p


: = lazy assignment:

https://go.dev/tour/basics/10

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

blank identifiers:

_ (https://www.educative.io/answers/what-is-the-blank-identifier-in-go) for unused variables

https://gobyexample.com/multiple-return-values

GO function returns two values 


package main
import "fmt"

The (int, int) in this function signature shows that the function returns 2 ints.

func vals() (int, int) {
    return 3, 7
}
func main() {

Here we use the 2 different return values from the call with multiple assignment.

    a, b := vals()
    fmt.Println(a)
    fmt.Println(b)

If you only want a subset of the returned values, use the blank identifier _.

    _, c := vals()
    fmt.Println(c)
}

No comments:

Post a Comment