golang
the one language you have to try in 2014
golang
the one language you have to try in 2014
Andrzej Grzesik
!
!
!
@ags313
andrzej@grzesik.it
andrzejgrzesik.info
about:me
dev going deeper
disclaimers
my opinions are my own
I hate computers
questions?
shoot!
golang
gopher
free and open source
BSD licensed
comes from G
FAST
and I mean FAST
tl;dr;
C++ and ruby had a wild time
play with it tonight
so, why do I like go?
no runtime dependencies!
more pleasant than C
go toolchain
go command
most important thing
there is only one formatting
package main!
!
import "fmt"!
!
func main() {!
!fmt.Println("Hello world")!
}!
types
types
• uint8, uint16, uint32, uint64
• int8, int16, int32, int64
• float32, float64
• complex64, complex128
• byte alias fo...
func program() {!
var text!
text = “zomg"!
more := "zomg"!
!
fmt.Println(len(text));!
}!
maps
func main() {!
attendees := map[string]bool{!
"Phil": true,!
"Marcin": true,!
}!
!
fmt.Println(attendees["Phil"]) // true!...
structs
type Rectangle struct {!
a, b int32!
}!
!
func main() {!
var rect Rectangle!
rect = Rectangle{5, 10}!
rect = Rectangle{a: ...
type Square struct {!
side int32!
}!
!
func (sq Square) Area() int32 {!
return sq.side * sq.side!
}!
!
func main() {!
s :=...
interfaces
type Square struct {!
side int32!
}!
!
func (sq Square) Area() int32 {!
return sq.side * sq.side!
}!
!
type HasArea interf...
goroutines
lightweight threads
func f(i int) {!
amt := rand.Intn(1000)!
time.Sleep(time.Duration(amt) * time.Millisecond)!
fmt.Println(i)!
}!
!
func main...
how many will run?
runtime.GOMAXPROCS(4)
channels
channels
• communicate between funcs
• typed
• thread-safe
channels
channel := make(chan int)!
unbuffered channels
• sync
• will wait when empty
buffered channels
channel := make(chan int, size)!
buffered channels
• async
• return 0 element when empty
• will only wait when full
basics
channel := make(chan int)!
c <- a!
!
<- c!
!
a = <- c!
!
a, ok = <- c!
func program() {!
channel := make(chan int) !
}!
!
func from(connection chan int) {!
connection <- rand.Intn(255)!
}!
!
fu...
but that’s not cool yet
coordinate routines
func program() {!
channel := make(chan int) !
!
go func() {!
close(channel)!
// or!
channel <- anything!
}()!
!
<- channel...
func program() {!
latch := make(chan int) !
!
go worker()!
close(latch)!
}!
!
func worker() {!
<- latch !
}!
generators
id := make(chan int64)!
go func() {!
var counter int64 = 0!
for {!
id <- counter!
counter += 1!
} !
}()
multiple channels at once!
func program() {!
select {!
case a := <- channel!
!
case b, mkay := other!
!
case output <- z!
!
default:!
}!
}!
ranges
func fillIn(channel chan int) {!
channel <- 1!
channel <- 2!
channel <- 4!
close(channel)!
}!
!
func main() {!
channel := ...
packages
[18:48][agrzesik@melmac:~/vcs/talks/go/hello]!
$ find .!
.!
./bin!
./bin/main!
./pkg!
./pkg/darwin_amd64!
./pkg/darwin_amd...
import (!
"code.google.com/p/go.net/websocket"!
"fmt"!
"net/http"!
)!
go get
net
echo server
const listenAddr = "localhost:4000"!
!
func main() {!
l, err := net.Listen("tcp", listenAddr)!
if err != nil {!
log.Fatal(...
concurrent echo server
const listenAddr = "localhost:4000"!
!
func main() {!
l, err := net.Listen("tcp", listenAddr)!
if err != nil {!
log.Fatal(...
const listenAddr = "localhost:4000"!
!
func main() {!
l, err := net.Listen("tcp", listenAddr)!
if err != nil {!
log.Fatal(...
websockets?
func main() {!
http.Handle("/", websocket.Handler(handler))!
http.ListenAndServe("localhost:1984", nil)!
}!
!
func handler...
so, what looks bad?
type AssetMetas struct {!
!Metas []AssetMeta `json:"assetMetas"`!
}!
!
type AssetMeta struct {!
!ResourceName string `json...
so, go code!
Upcoming SlideShare
Loading in …5
×

Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

754 views
654 views

Published on

You live and breathe http. Most of the things you do with a computer involve it.
How many tiny little http-related utils have you already created?
Is it time to stop? By no means, we all do it.
I'd just like to encourage you to write the next one in Go.
It's a fun little language, which feels like a cross-breed between C (pointers!)
and ruby (concise, powerful syntax). But how much?

Why, yet another language, but my perl/python/.. does all the things?
Well, it does. But go has a few things that make it super sweet,
is web-scale and real fun to use!

Or at least come and listen how did we get rid of 87% of our codebase in a day :-)

Andrzej Grzesik - I like programming. I do it a lot, mostly on the JVM, usually writing fancy backends for big, distributed systems.

I also display a particular affection to continuous delivery.. UI, unless quickly hacked, is not my play ;-) I believe that most problems we deal with are people problems, so I mix and match tools with technologies to achieve my goals, make people happy and achieve world peace :-) I believe in software quality, and organize GeeCON, Polish JUG, Krakow Software Craftsmanship, Cracow Hadoop User Group. In my free time, I read paper books and cycle, a lot!

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
754
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Atmosphere 2014: Go, go, go - the one language to try in 2014. (or: "write only an eight of the code" ;-)) - Andrzej Grzesik

  1. 1. golang the one language you have to try in 2014
  2. 2. golang the one language you have to try in 2014
  3. 3. Andrzej Grzesik ! ! ! @ags313 andrzej@grzesik.it andrzejgrzesik.info
  4. 4. about:me
  5. 5. dev going deeper
  6. 6. disclaimers
  7. 7. my opinions are my own
  8. 8. I hate computers
  9. 9. questions? shoot!
  10. 10. golang
  11. 11. gopher
  12. 12. free and open source
  13. 13. BSD licensed
  14. 14. comes from G
  15. 15. FAST and I mean FAST
  16. 16. tl;dr; C++ and ruby had a wild time
  17. 17. play with it tonight
  18. 18. so, why do I like go?
  19. 19. no runtime dependencies!
  20. 20. more pleasant than C
  21. 21. go toolchain
  22. 22. go command
  23. 23. most important thing
  24. 24. there is only one formatting
  25. 25. package main! ! import "fmt"! ! func main() {! !fmt.Println("Hello world")! }!
  26. 26. types
  27. 27. types • uint8, uint16, uint32, uint64 • int8, int16, int32, int64 • float32, float64 • complex64, complex128 • byte alias for uint8 • rune alias for int32 • string
  28. 28. func program() {! var text! text = “zomg"! more := "zomg"! ! fmt.Println(len(text));! }!
  29. 29. maps
  30. 30. func main() {! attendees := map[string]bool{! "Phil": true,! "Marcin": true,! }! ! fmt.Println(attendees["Phil"]) // true! fmt.Println(attendees["ags"]) // false! partygoers["ags"] = true! fmt.Println(attendees["ags"]) // true! }!
  31. 31. structs
  32. 32. type Rectangle struct {! a, b int32! }! ! func main() {! var rect Rectangle! rect = Rectangle{5, 10}! rect = Rectangle{a: 10, b: 5}! ! HasArea(s).Area()! }
  33. 33. type Square struct {! side int32! }! ! func (sq Square) Area() int32 {! return sq.side * sq.side! }! ! func main() {! s := Square{16}! area := s.Area()! }
  34. 34. interfaces
  35. 35. type Square struct {! side int32! }! ! func (sq Square) Area() int32 {! return sq.side * sq.side! }! ! type HasArea interface {! Area() int32! }! ! func main() {! s := Square{16}! HasArea(s).Area()! }
  36. 36. goroutines lightweight threads
  37. 37. func f(i int) {! amt := rand.Intn(1000)! time.Sleep(time.Duration(amt) * time.Millisecond)! fmt.Println(i)! }! ! func main() {! for i := 0; i < 3; i++ {! go f(i)! }! var input string! fmt.Scanln(&input)! }
  38. 38. how many will run? runtime.GOMAXPROCS(4)
  39. 39. channels
  40. 40. channels • communicate between funcs • typed • thread-safe
  41. 41. channels channel := make(chan int)!
  42. 42. unbuffered channels • sync • will wait when empty
  43. 43. buffered channels channel := make(chan int, size)!
  44. 44. buffered channels • async • return 0 element when empty • will only wait when full
  45. 45. basics channel := make(chan int)! c <- a! ! <- c! ! a = <- c! ! a, ok = <- c!
  46. 46. func program() {! channel := make(chan int) ! }! ! func from(connection chan int) {! connection <- rand.Intn(255)! }! ! func to(connection chan int) {! i := <- connection! fmt.Println(“much received", i)! }!
  47. 47. but that’s not cool yet
  48. 48. coordinate routines
  49. 49. func program() {! channel := make(chan int) ! ! go func() {! close(channel)! // or! channel <- anything! }()! ! <- channel! }!
  50. 50. func program() {! latch := make(chan int) ! ! go worker()! close(latch)! }! ! func worker() {! <- latch ! }!
  51. 51. generators
  52. 52. id := make(chan int64)! go func() {! var counter int64 = 0! for {! id <- counter! counter += 1! } ! }()
  53. 53. multiple channels at once!
  54. 54. func program() {! select {! case a := <- channel! ! case b, mkay := other! ! case output <- z! ! default:! }! }!
  55. 55. ranges
  56. 56. func fillIn(channel chan int) {! channel <- 1! channel <- 2! channel <- 4! close(channel)! }! ! func main() {! channel := make(chan int)! go fillIn(channel)! ! for s := range channel {! fmt.Printf("%d n", s)! }! }
  57. 57. packages
  58. 58. [18:48][agrzesik@melmac:~/vcs/talks/go/hello]! $ find .! .! ./bin! ./bin/main! ./pkg! ./pkg/darwin_amd64! ./pkg/darwin_amd64/hello.a! ./src! ./src/hello! ./src/hello/hello.go! ./src/main! ./src/main/.main.go.swp! ./src/main/main.go!
  59. 59. import (! "code.google.com/p/go.net/websocket"! "fmt"! "net/http"! )!
  60. 60. go get
  61. 61. net
  62. 62. echo server
  63. 63. const listenAddr = "localhost:4000"! ! func main() {! l, err := net.Listen("tcp", listenAddr)! if err != nil {! log.Fatal(err)! }! for {! c, err := l.Accept()! if err != nil {! log.Fatal(err)! }! io.Copy(c, c)! }! }
  64. 64. concurrent echo server
  65. 65. const listenAddr = "localhost:4000"! ! func main() {! l, err := net.Listen("tcp", listenAddr)! if err != nil {! log.Fatal(err)! }! for {! c, err := l.Accept()! if err != nil {! log.Fatal(err)! }! go io.Copy(c, c)! }! }
  66. 66. const listenAddr = "localhost:4000"! ! func main() {! l, err := net.Listen("tcp", listenAddr)! if err != nil {! log.Fatal(err)! }! for {! c, err := l.Accept()! if err != nil {! log.Fatal(err)! }! io.Copy(c, c)! }! }
  67. 67. websockets?
  68. 68. func main() {! http.Handle("/", websocket.Handler(handler))! http.ListenAndServe("localhost:1984", nil)! }! ! func handler(c *websocket.Conn) {! var s string! fmt.Fscan(c, &s)! fmt.Println("Received:", s)! fmt.Fprint(c, “hey!”)! }!
  69. 69. so, what looks bad?
  70. 70. type AssetMetas struct {! !Metas []AssetMeta `json:"assetMetas"`! }! ! type AssetMeta struct {! !ResourceName string `json:"resource_name"`! !Md5 string `json:"md5"`! !Urls []string `json:"urls"`! }!
  71. 71. so, go code!

×