README
¶
react-go-tutorial
To-do-list - Reactjs, Golang, Typescript, MongoDB
- go install github.com/cosmtrek/air@latest - https://github.com/air-verse/air
- go get github.com/joho/godotenv
- go get go.mongodb.org/mongo-driver/mongo
- https://railway.app/new -- Deploy
Equivalent of npm init in Go
go mod init <module_name>
go mod initis used to initialize a new module.- It creates a new
go.modfile in the current directory. - The
go.modfile contains information about the module, its dependencies, and the Go version.
Equivalent of npm run start in Go
go run <main_file.go>
go runis used to compile and run a Go program.- It compiles the program and executes it.
Equivalent of npm install <package_name> in Go
go get <package_name>
go getis not a package manager.go getis used to download and install packages from remote repositories.- It does not handle versioning.
- This command fetches the package and its dependencies (if any)
Equivalent of package.json in Go
go.mod file
- It contains information about the module, its dependencies, and the Go version.
Equivalent of npm install in Go
go mod tidy
go mod tidyis used to add missing and remove unused modules.- It updates the go.mod file to use the latest version of the dependencies.
Equivalent of JSON.stringify() in Go
import "encoding/json"
func main() {
data := map[string]interface{}{
"name": "John Doe",
"age": 30,
}
jsonString, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(jsonString))
}
json.Marshal()is used to convert a Go data structure to a JSON string.
Equivalent of JSON.parse() in Go
import "encoding/json"
func main() {
jsonString := `{"name":"John Doe","age":30}`
var data map[string]interface{}
err := json.Unmarshal([]byte(jsonString), &data)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(data)
}
json.Unmarshal()is used to convert a JSON string to a Go data structure.
Equivalent of nodemon in Go
go install github.com/cosmtrek/air@latest
airis a live reload tool for Go applications.- It watches for file changes and automatically rebuilds and restarts the application.
- It is similar to
nodemonin the Node.js ecosystem. - There are other tools like
freshwhich can also be used for live reloading in Go.
Equivalent of dotenv in Go
go get github.com/joho/godotenv
godotenvis a Go package that loads environment variables from a.envfile.- It is similar to
dotenvin the Node.js ecosystem. - It allows developers to store sensitive information like API keys, database URIs, etc., in a
.envfile and load them into the application.
Code Example of Using godotenv
package main
import (
"fmt"
"log"
"os"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
MONGODB_URI := os.Getenv("MONGODB_URI")
}
- In this example, we load environment variables from a
.envfile usinggodotenv.Load(".env"). - We can then access the environment variables using
os.Getenv("MONGODB_URI").
Equivalent of Express.js in Go
go get github.com/gofiber/fiber/v2
Fiberis a web framework for Go that is inspired by Express.js.- It is fast, lightweight, and easy to use.
- It provides a similar API to Express.js, making it easy for developers familiar with Express.js to transition to Go.
- Other popular web frameworks in Go include
GinandEcho.
Equivalent of Express.js Middleware in Go
func main() {
app := fiber.New()
app.Use(middleware)
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
}
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Middleware logic
// .
// .
// .
next.ServeHTTP(w, r)
})
}
- In this example, we define a middleware function that takes the
nexthandler as an argument. - The middleware function wraps the
nexthandler and executes some logic before calling thenexthandler.
Equivalent of Express.js Route Handling in Go
func main() {
app := fiber.New()
app.Get("/", helloHandler)
app.Listen(":3000")
}
func helloHandler(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
}
- In this example, we define a route handler function
helloHandlerthat sends a response back to the client. - We then register the route handler with the
app.Get()method, specifying the route path and the handler function.
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.