Go Programming Blueprints reading notes (talked about NSQ/MGO processing data persistence, but the business logic is not complex enough)

Source: Internet
Author: User
Tags install mongodb key string mongodb driver
This is a creation in Article, where the information may have evolved or changed.

Go Programming Blueprints

  1. http. Handle ("/", &templatehandler{filename: "chat.html"});
    1. http. Handle static method?
    2. function object parameters with parameters? It's an ordinary struct--. Why don't you need new?
  2. Go get Github.com/gorilla/websocket (Easy package dependent management!) )
  3. Go statement not required; end of Tag
  4. TDD: Assuming that a type struct is already present before it is defined?
  5. Control Flow: This is similar to Swift,if/for no need to add ()
  6. Initialization/Assignment (: =) vs (<-) Chan stream as output right input (to left can be considered a unit operation?) )
  7. Special syntax for the map type: Map[keytype]valuetype---you can only think that the map in go is treated as a built-in type
    1. Therefore, it is not necessary to define the Equals/hashcode method of the Key object class like Java, but also to implement the Map container class as a template library (non-language feature).
    2. Can Chan type also be considered as such?
    3. Put Chan/map in front of the type, a bit of Haskell style?
  8. What Chan can think of as a conduit for message communication between go routine? Must the same type be said in each of the Chan's messages?
    1. The definition within the struct is join Chan *client, but is used for {select {case client: = <-r.join: ...}}, the programming style is too weird.
      1. Can only think of the * in go inside the pointer type, should be understood as a reference? and access its members using. Instead of
      2. The for-select here seems to draw on the idiomatic approach of Erlang
    2. Create Chan:make (chan []byte)//not called New call make~
  9. Any parameter: a ... interface{}
  10. Define a (Member) method for a struct? It feels like a objective-c.
  11. Certification: Handler wrapper (design mode)
  12. Processing file uploads: filename, header, err: = req. Formfile ("Avatarfile")//where is the file saved? Multipart. File interface type?
    1. Data, err: = Ioutil. ReadAll (file)
    2. The more generic the interface, the better-but does the standard library ensure that all the common ones are defined?
  13. Net/http the file server that is built into the package:
    1. http. Handle ("/avatars/", http. Stripprefix ("/avatars/", http. Fileserver (http. Dir ("./avatars"))))
  14. Bufio
    1. S: = Bufio. Newscanner (OS. Stdin)//may be scanned using regular expressions?
    2. For S.scan () {...}//Let me always think of the Java StringTokenizer class
  15. OS/EXEC: Connecting multiple processes with pipelines
    1. Exec.Command:nextCmd.Stdin = Thiscmd.stdoutpipe (), then call the start () method of the Cmd object and Wait ()
  16. NSQ
    1. Install driver: Go get github.com/bitly/go-nsq
    2. Install MongoDB driver: Go get gopkg.in/mgo.v2
      1. Mongod--dbpath./db
      2. ITER: = db. DB ("ballots"). C ("polls"). Find (nil). Iter ()
  17. TCP Long connections
    1. NETC, err: = Net. Dialtimeout (NETW, addr, 5*time. Second)//go The name of the real TM heck, dial?
    2. HttpClient = &http. client{
      Transport: &http. transport{
      dial:dial,//Use the custom Dial method (? )
      },}
  18. Environment variables: ' env: ' sp_twitter_key,required '
  19. Fmt. Println (reflect. TypeOf(struct{}{}). Size ())//=0
  20. Stopchan <-chan struct{}//Only accepted channel? This syntax is so weird that the operator can be part of a type signature
  21. Capturing OS Signals
    1. Termchan: = Make (chan os. Signal, 1)
    2. Signal. Notify (Termchan, Syscall. SIGINT, Syscall. SIGTERM, Syscall. SIGHUP)
  22. Implementing RESTful APIs
    1. var vars map[*http. request]map[string]interface{}
    2. var varslock sync. Rwmutex//go The use of read-write locks inside, performance is not a problem?
    3. See also Wrapper:func Withapikey (fn http. Handlerfunc) http. Handlerfunc {...//the high-order function has been used here (Python adorner?). )
      1. Withcors (Withvars (withdata (DB, Withapikey (handlepolls))))
  23. The facade interface exposes a single public method, which would return the public view of a struct
  24. Q.lat, _ = StrConv. Parsefloat (req. Url. Query (). Get ("Lat"), 64)//library API designed to be concise
  25. var ZIP archiver = (*zipper) (nil)//compiler performs interface type consistency check
    1. var _ Interface = (*implementation) (nil)
    2. If err: = os. Mkdirall(filepath. Dir (dest), 0777); Err! = Nil {...
    3. Dirhash: Essentially converting a directory to a key string object, using a comparison of strings instead of a comparison of the directory attributes (decoupled)
  26. Case <-Time. after (time. Duration (*interval) * time. Second): ...//hmm? Returns a channel that will send the specified timeout message
  27. Gopath//equivalent to the repo concept in Maven, or Java_home?
    1. Go get code.google.com/p/go.tools/cmd/goimports//hmm? Why not built-in

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.