The Solution: Raw Queries with ORMs
You can use raw SQL queries within an ORM like GORM to combine the best of both worlds. Hereโs how:
1. Optimize Critical Queries: Use raw SQL for performance-critical operations.
2. Handle Complex Queries: Tackle complex queries or specific database features not directly supported by the ORM.
3. Leverage ORM Benefits: Use GORMโs parameter binding and security features alongside raw SQL.
Example (using GORM):
var users []User db.Raw("SELECT * FROM users WHERE age > ?", 30).Scan(&users)
Raw SQL Queries vs ORM: Finding the Balance
Raw SQL Queries:
Pros: High performance, full control over the query.
Cons: Requires deep understanding of queries, harder to maintain, risk of SQL injection.
ORM (e.g., GORM):
Pros: Easy to use, boosts productivity, automatic security.
Cons: Less efficient, complex queries can be challenging.
6. Storing User Validation StatusLogin with OTP Once the OTP is successfully validated, update the userโs record in the database to reflect that they have been authenticated.
Feel free to share you thoughts on improving this approach.
Login with OTP
1. Setting Up the /login Endpoint
When a user attempts to log in via the /login endpoint, the process involves generating an OTP, storing it with an expiration time, and then validating the OTP to authenticate the user.
4. Sending OTP to User After generating the OTP, send it to the user via their preferred method (email or SMS). You can use services like SendGrid for email or Twilio for SMS.
3. Generate the OTP and Store It Youโll need a function to generate the OTP and store it in the database along with a timestamp indicating its expiration.
okay is this some glitch or i am doing something wrong, the other responses are coming in JSON, but not this JSON binding error, its returning in plain text in postman.
In a real-world scenario, when using Go to handle JSON data, you often have a structure that you need to populate with values from incoming JSON.
Here the &creds is a pointer to a struck names creds,
allowing BindJSON to modify it directly
Pointers in Go
In Go, a pointer is a variable that stores the memory address of another variable. It points to the memory location where the value is stored.
Why use pointers?
1. Efficient memory management:
- It passes the memory address instead of copying the entire structure, which is more efficient.
2. Modifying variables:
- When you pass a variable to a function via a pointer, you can modify the original value as the function operates on the actual memory address of the variable.
How to use pointers
1. Creating a pointer to a variable:
- Syntax: var (variable name of the pointer) *(type of variable) = &(variable to which you want to point)
- Example:
var number int = 99
var pointer *int = &number
2. Accessing the value of the variable via pointer:
- Syntax: *(variable name of the pointer)
- Example:
fmt.Println(*pointer) // Outputs: 99
3. Modifying the value via pointer:
- Example:
*pointer = 100
fmt.Println(number) // Outputs: 100
Github repo link below
https://t.co/esnTl28ktc
@That_Tech_Savvy That's impressive which tech stack did you use? I am also planning to create my own chat app with the features I really want to use not just texting and normal stuff.