Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. Learning Go
  2. With Adrian
  3.  
  4. https://github.com/solarwinds/golesson
  5.  
  6. There’s only ONE way to format go.
  7.  
  8. Go is simple:
  9. - Lowercase variables cannot be accessed via MAIN.
  10. - Uppercase is global.
  11.  
  12. At branch: lesson2/0-http-handler, you can go to http://localhost:7777/hello and returns an http response.
  13.  
  14.  
  15. GO’s standard library is pretty damn good. Use their standard HTTP library.
  16.  
  17. Small fact: since HTTP library is so good, lots of GO middleware just interfaces and plugs in and gets called by HTTP library. Any third party functions can just be called.
  18.  
  19. Why do we write a function to return a function? Instead of just returning a function.
  20. - Go functions are closures?
  21.  
  22. Branch: lesson2/2-channel-for-sigint
  23. “The hello world of channel usage”
  24. a channel is a conduit of sending values from one go routine to another go routine. You always have a MAIN go routine happening — but when u use the keyword ‘go’ anywhere, it starts ANOTHER routine.
  25.  
  26. Channels are typed. U can only say one type of thing in that channel.
  27.  
  28. GO’s concept is to not introduce radical new concepts — but to stay familiar.
  29. arrows mean something.<- is reading from. -> is coming into
  30. The arrows will block the channel until something is there to read. Can be bad.
  31.  
  32. var sigIntChan = make(chan os.Signal, 1)
  33. signal.Notify(sigIntChan, os.Interrupt)
  34. go processSigInt()
  35.  
  36.  
  37. GO is not object oriented. does not thing objects are classes.thinks of types and interfaces.
  38.  
  39. multiplexer (MUX) 
  40.  
  41. “I had a foobar type” go terminology.
  42.  
  43. “MARSHAL” - like serializing. It turns struct into a json string. He said, u can write your own encoder but 99% of the time, Marshal will be more than enough bc go is very performant.
  44. Error is a built in interface. He says keep it simple, just turn a value and an error.
  45. This is beginner friendly error pattern:
  46. if err != nil {
  47. w.WriteHeader(http.StatusInternalServerError)
  48. return
  49. }
  50. He really likes this. Forces you to handle every error. What if json doesn’t marshal?
  51. Json Marshall returns a slice of bytes. “Slice” is like an array.
  52. Should get into a habit of adding a ‘return’ just incase.
  53.  
  54. TESTING
  55. Most people just use the standard Testing library. Go will recognize all functions that starts with the name Test.
  56.  
  57.  
  58. Go thing to do “cross compile a library”. When a library doesn’t work in one environment — he got into go when he was trying to cross compile python into C on a raspberry pi. Heard that Go can do it. Fell in <3.
  59.  
  60. Theres only one loop in go. The for loop. With nothing in it, it is infinite. There are several flavors of for loop. 1. select.
  61. Using a Label busts u out of a for loop. *he mixes past this cuz its nbd. Just tell nurself, u got out of the loop!*
  62.  
  63. Use GO for high speed applications
  64.  
  65. Set up the things that READ before you set up the things that WRITE. Because server will hang forever if u mess up.
  66.  
  67. Branch: lesson5/2-tuneable-stop-with-bug. go run main.go -friendly -time-limit=10 for crazy product managers who want something to exist for a limited duration.
  68. Bug is u must close the channel for the garbage collector. Will shut down whatever is trying to write to the channel.
  69.  
  70. Branch: lesson5/3-tuneable-stop-fixed
  71. Added a variable called stopProcessing in web.go
  72. Flips a flag to return a 404
  73.  
  74. Go - “black eye go” book. Cross compiling to make malware. Will give u a skeleton code for u to write malware with that will fit. Can interface with any OS.
  75. Port scanners/sniffers. CNC, network traffic.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement