Code Monkey home page Code Monkey logo

first-go-api's Introduction

Creating simplest API in GO

Download and Install Go

https://golang.org/dl/

Directory Structure

Create a folder 'go' at your home directory as $HOME/go is the default $GOPATH. Create 'src' and 'bin' directory within 'go' directory. Packages reside inside 'src' directory.

Now, run the follwoing command to get this project with all dependency packages:

go get github.com/shoeb240/first-go-api

OR

Create the following directory structure within $HOME/go directory:

bin/
src/
    github.com/shoeb240/	

cd to src/github.com/shoeb240/ and clone this project from github with the following git command:

git clone http://github.com/shoeb240/first-go-api

To download Gorilla Mux pacjake run the following command:

go get github.com/gorilla/mux

Now, you will have the following directory structure:

bin/
src/
    github.com/gorilla/
	    mux/
    github.com/shoeb240/
	    first-go-api/
	        main.go               # command source
	        route.go               
	        handlers.go               

route.go

Creating api route '/users' and opening port 8081 to listen to request using gorilla mux

package main

import (
    "github.com/gorilla/mux"
    "net/http"
)

func RequestHandle() {
    myRouter := mux.NewRouter()
    myRouter.HandleFunc("/users", getUsers).Methods("GET")

    http.ListenAndServe(":8081", myRouter)
}

handlers.go

Creating getUsers handler method to respond to the route

package main

import (
    "fmt"
    "net/http"
)

func getUsers(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Showing users list")
}

main.go

Calling RequestHandle() from main

package main

import (
    "fmt"
)

func main(){
    fmt.Println("Waiting for request to serve...")
    RequestHandle()
}

Run the following command

go run github.com/shoeb240/first-go-api

It will output

Waiting for request to serve...

Open your browser to open url 'localhost:8081/users'. It will show..

I am responding to your API call

first-go-api's People

Contributors

shoeb240 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.