Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shoeb240/first-go-api
Creating first go api project as I am learning golang
https://github.com/shoeb240/first-go-api
api go golang gorilla-mux rest-api restful-api
Last synced: 3 months ago
JSON representation
Creating first go api project as I am learning golang
- Host: GitHub
- URL: https://github.com/shoeb240/first-go-api
- Owner: shoeb240
- Created: 2018-12-20T13:17:27.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-21T06:23:07.000Z (almost 6 years ago)
- Last Synced: 2024-05-18T01:58:47.852Z (6 months ago)
- Topics: api, go, golang, gorilla-mux, rest-api, restful-api
- Language: Go
- Size: 3.91 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-golang-repositories - first-go-api
README
# 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:
```cgo
go get github.com/shoeb240/first-go-api
```OR
Create the following directory structure within $HOME/go directory:
```cgo
bin/
src/
github.com/shoeb240/
```cd to src/github.com/shoeb240/ and clone this project from github with the following git command:
```cgo
git clone http://github.com/shoeb240/first-go-api
```To download Gorilla Mux pacjake run the following command:
```cgo
go get github.com/gorilla/mux
```Now, you will have the following directory structure:
```cgo
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```go
package mainimport (
"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```go
package mainimport (
"fmt"
"net/http"
)func getUsers(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Showing users list")
}
```## main.go
Calling RequestHandle() from main```go
package mainimport (
"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
```