MrDongdongLin / Learn-Go

This repository records some skills about Go programming.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Learn-Go

Build Status Packagist Powerby

This repository records some skills about Go programming.

This project describes how to read a yaml file and print it to screen.

  • Package need to import:
    • io/ioutil
    • gopkg.in/yaml.v2
  • Read yaml file:
    conf, err := ioutil.ReadFile("conf.yaml")
  • Convert yaml data to struct:
    err = yaml.Unmarshal(conf, &c)

where c is a struct object that defined according to the format of yaml data by us. Suppose yaml data is

person:
  name: 'Flower'
  age:  '25'

then our struct must define as

type Conf struct {
  Person struct {
    Name string `yaml:"name"`
    Age  string `yaml:"age"`
  }
}

HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go.

Installation

go get github.com/julienschmidt/httprouter

Create a new httprouter object with New function, and we can create request methods like GET, POST and so on. Let's see an example:

router := httprouter.New()
router.GET("/get/:requestid", controller.GetTaskProcess)
router.POST("/post/:requestid", controller.PostTaskProcess)
...

then writing methods GetTaskProcess and PostTaskProcess with a simple response respectively:

func GetTaskProcess(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  fmt.Fprintf(w, "hello, %s!\n", ps.ByName("requestid"))
}

func PostTaskProcess(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  body, err := ioutil.ReadAll(r.Body)
  if err != nil {
    fmt.Fprint(w, "hello, something wrong happened for sending message to %s!\n", ps.ByName("requestid"))
  } else {
    fmt.Fprint(w, "Your enter is %s\n", []byte(body))
  }
}

At last, we use curl to test this project:

curl -XGET localhost:8002/get/world
curl -XPOST localhost:8002/post/hello

then we can get hello, world! and Your enter is hello on the screen respectively.

The fantastic ORM library for Golang, aims to be developer friendly.

Installation

go get -u github.com/jinzhu/gorm

Ditail operations can be found here.

Here, taking the gosexy redis for example, to introduce how to use redis in golang.

Installation

  • redis
wget http://download.redis.io/releases/redis-2.8.3.tar.gz
tar xzf redis-2.8.3.tar.gz
cd redis-2.8.3
make
./redis-server
  • redis client
go get menteslibres.net/gosexy/redis

then import redis with

import "menteslibres.net/gosexy/redis"
  • usage
var client *redis.Client
client = redis.New()
err = client.Connect(host, port)
if err != nil {
  log.Fatalf("Connect failed: %s\n", err.Error())
  return
}
log.Println("Connected to redis-server.")
log.Printf("Sending PING...\n")
s, err = client.Ping()
if err != nil {
  log.Fatalf("Could not ping: %s\n", err.Error())
  return
}
log.Printf("Received %s!\n", s)
client.Quit()

A RESTful api using httprouter and redis

REST (Representational State Transfer) is a uniform way of locating resources on the internet that goes beyond the boundaries of programming languages, more information about this project can be found here.

CHANGELOG

CHANGELOG

Contact me

About

This repository records some skills about Go programming.


Languages

Language:Go 100.0%