User Directory Project – GinGonic golang framework 13
User Directory Project – GinGonic golang framework 13
Source Code
[sourcecode lang=”go” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false” title=”Source Code main.go”]// Let Create User directory / Phone Directory // book finding/search system
package main
import "github.com/gin-gonic/gin"
func main() {
type User struct {
Username string
City string
Phone string
// every first letter should be capital
}
// this end of our STRUCT.. struct is custom variable
// collection of different variables and not data
// lets create our database
data_users := []User{
// enter data in our database/ slice/array
{"ali", "dehli", "321321321"},
{"sai", "kabul", "421321321"},
{"alina", "dehli", "821321321"},
{"alishba", "Madina", "921321321"},
{"alia", "Newyork", "721321321"},
// our data entry is complete.. lets focus on search
}
// create server
r := gin.Default()
// Get request & query string
// user… city… phone#
r.GET("/", func(c *gin.Context) {
user := c.Query("user")
city := c.Query("city")
phone := c.Query("phone")
// lets create our users database
// but we need struct first
// range .. loop through database
for _, userdata := range data_users {
if user == userdata.Username || city == userdata.City || phone == userdata.Phone {
c.String(200, "Hello %s from %s and phone # %s",
userdata.Username, userdata.City, userdata.Phone)
// return to end loop
return
}
// if no data found
}
c.String(200, "No User Found…")
})
r.Run()
}
[/sourcecode]
golang gingonic framework videos tutorials series
TAGS#
gin gonic tutorial
gin-gonic / examples
gin-gonic documentation
gin framework tutorial
gin web framework
gin router
golang gin context
gin router group
###Buy Me a Coffee https://www.buymeacoffee.com/khan1