This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type Point struct{ | |
| x int | |
| y int | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type Person struct{ | |
| name string | |
| socialSecurityNumber string | |
| age uint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "bytes" | |
| "database/sql" | |
| "fmt" | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| _ "github.com/go-sql-driver/mysql" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "fmt" | |
| _ "github.com/go-sql-driver/mysql" | |
| ) | |
| func main() { | |
| db, err := sql.Open("mysql", "root:passapp@tcp(127.0.0.1:3306)/gotest") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // GET a person detail | |
| router.GET("/person/:id", func(c *gin.Context) { | |
| var ( | |
| person Person | |
| result gin.H | |
| ) | |
| id := c.Param("id") | |
| row := db.QueryRow("select id, first_name, last_name from person where id = ?;", id) | |
| err = row.Scan(&person.Id, &person.First_Name, &person.Last_Name) | |
| if err != nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "database/sql" | |
| "fmt" | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| _ "github.com/go-sql-driver/mysql" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // GET all persons | |
| router.GET("/persons", func(c *gin.Context) { | |
| var ( | |
| person Person | |
| persons []Person | |
| ) | |
| rows, err := db.Query("select id, first_name, last_name from person;") | |
| if err != nil { | |
| fmt.Print(err.Error()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // POST new person details | |
| router.POST("/person", func(c *gin.Context) { | |
| var buffer bytes.Buffer | |
| first_name := c.PostForm("first_name") | |
| last_name := c.PostForm("last_name") | |
| stmt, err := db.Prepare("insert into person (first_name, last_name) values(?,?);") | |
| if err != nil { | |
| fmt.Print(err.Error()) | |
| } | |
| _, err = stmt.Exec(first_name, last_name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Delete resources | |
| router.DELETE("/person", func(c *gin.Context) { | |
| id := c.Query("id") | |
| stmt, err := db.Prepare("delete from person where id= ?;") | |
| if err != nil { | |
| fmt.Print(err.Error()) | |
| } | |
| _, err = stmt.Exec(id) | |
| if err != nil { | |
| fmt.Print(err.Error()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "database/sql" | |
| "fmt" | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| _ "github.com/go-sql-driver/mysql" |