User Directory Project – GinGonic golang framework 13

User Directory Project – GinGonic golang framework 13

Source Code

<div>
<div>// Let Create User directory / Phone Directory // book finding/search system</div>
<div>package main</div>
<div>import "github.com/gin-gonic/gin"</div>
<div>func main() {</div>
<div>    type User struct {</div>
<div>        Username string</div>
<div>        City     string</div>
<div>        Phone    string</div>
<div>        // every first letter should be capital</div>
<div>    }</div>
<div>    // this end of our STRUCT.. struct is custom variable</div>
<div>    // collection of different variables and not data</div>
<div>    // lets create our database</div>
<div>    data_users := []User{</div>
<div>        // enter data in our database/ slice/array</div>
<div>        {"ali", "dehli", "321321321"},</div>
<div>        {"sai", "kabul", "421321321"},</div>
<div>        {"alina", "dehli", "821321321"},</div>
<div>        {"alishba", "Madina", "921321321"},</div>
<div>        {"alia", "Newyork", "721321321"},</div>
<div>        // our data entry is complete.. lets focus on search</div>
<div>    }</div>
<div>    // create server</div>
<div>    r := gin.Default()</div>
<div>    // Get request &amp; query string</div>
<div>    // user... city... phone#</div>
<div>    r.GET("/", func(c *gin.Context) {</div>
<div>        user := c.Query("user")</div>
<div>        city := c.Query("city")</div>
<div>        phone := c.Query("phone")</div>
<div>        // lets create our users database</div>
<div>        // but we need struct first</div>
<div>        // range .. loop through database</div>
<div>        for _, userdata := range data_users {</div>
<div>            if user == userdata.Username || city == userdata.City || phone == userdata.Phone {</div>
<div>                c.String(200, "Hello %s from %s and phone # %s",</div>
<div>                    userdata.Username, userdata.City, userdata.Phone)</div>
<div>                // return to end loop</div>
<div>                return</div>
<div>            }</div>
<div>            // if no data found</div>
<div>        }</div>
<div>        c.String(200, "No User Found...")</div>
<div>    })</div>
<div>    r.Run()</div>
<div>}</div>
</div>

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

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: