1024programmer Java Go common external package analysis

Go common external package analysis


1. gorilla/mux

import "github.com/gorilla/mux  "

gorilla/mux is a powerful router, small but stable and efficient. It can not only support regular routing but also match according to Method, header, host and other information

Usage example:

r := mux.NewRouter()

 //Different from http.ServerMux, mux.Router is a complete regular match and sets the routing path/  index/, if the access path/idenx/hello will return 404
 //Set the routing path to /index/access path/index will also report 404, you need to set r.StrictSlash(true), only /index/ and /index can match
  r.HandleFunc("/index/", func(w http.ResponseWriter, r *http.Request) {
     w.Write([]byte("root path"))

 //mux.Vars(r) will return all parameters parsed by the request (map[string]string)
 //Accessing /hello/ghbai will output hello ghbai
  r.HandleFunc("/hello/{name:[a-zA-Z]+}",  func(w http.ResponseWriter, r *http.Request) {
     w.Write([]byte(fmt.Sprintf(  "hello %s", mux.Vars(r)["name"])  ))

 http.Handle("/", r)

In the projects I have been involved with, there is such an application:

func (registry *MsgRegistry) RegisterHandlers(r *mux.Router) {
 //The registerHandlers method is:
 func registerHandlers(r *mux.Router) {
     glog.V(2).Info  ("register handlers")
 //Called when opening the service:
 func (s *GameServer) StartServer(dispatcher MsgDispatcher) {
     s.dispatcher = dispatcher
     r := mux.NewRouter()
     http.Handle("/", r)
     glog.Fatal(http.ListenAndServe(fmt.Sprintf("%v", bindHost), nil))

2. golang/glog

import "github.com/golang/glog  "

glog is the Golang version of the famous Google open source C++ log library glog. Glog is a lightweight log library that is easy to get started and does not require configuration files and is stable and efficient, but there are few customizable controls. glog mainly has the following characteristics:

glog has four log levels INFO <WARING <ERROR <FATAL. Different levels of logs are printed to different files. Low-level log files (INFO) will contain high-level log information (ERROR)
Pass the parameter –log_dir through the command line to specify the storage directory of the log file. The default is os.TempDir()
The log file can be cut according to the file size, but the log file cannot be cut according to the date
The log output format is fixed (Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg…) cannot be customized
Flag.Parse() needs to be called when the program starts to parse the command line parameters, and glog.Flush() needs to be called when the program exits to ensure Output the contents of the cache area to a file.
Usage examples:

func main() {
     //Initialize command line parameters

     //Called when exiting to ensure the log is written to the file
     defer glog.Flush()

     glog.Info("hello, glog")
     glog.Warning("warning glog")
     glog.Error("error glog")

     glog.Infof("info %d", 1)
     glog.Warningf("warning %d", 2)
     glog.Errorf("error %d", 3)

3. bson

package main

 import (
 type TestStruct struct {
     Name string
     ID int32
 func main() {
     data, err := bson.Marshal(&TestStruct{Name: "Bob"})
     if err != nil {
     fmt.Println("%q", data)
     value := TestStruct{}
     err2 := bson.Unmarshal(data, &value)
     if err2 != nil {
     fmt.Println("value:", value)
     mmap := bson.M{}
     err3 := bson.Unmarshal(data, mmap)
     if err3 != nil {
     fmt.Println("mmap:", mmap)

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/768485

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top