| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 <html> | 1 <html> |
| 2 <head> | 2 <head> |
| 3 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > | 3 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > |
| 4 <title>A Tour of Go</title> | 4 <title>A Tour of Go</title> |
| 5 | 5 |
| 6 <!-- jQuery --> | 6 <!-- jQuery --> |
| 7 <script src="static/jquery.js"></script> | 7 <script src="static/jquery.js"></script> |
| 8 | 8 |
| 9 <!-- Fonts --> | 9 <!-- Fonts --> |
| 10 <link href='http://fonts.googleapis.com/css?family=Droid+Serif&v1' rel='styleshe et' type='text/css'> | 10 <link href='http://fonts.googleapis.com/css?family=Droid+Serif&v1' rel='styleshe et' type='text/css'> |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 } | 298 } |
| 299 </div> | 299 </div> |
| 300 </div> | 300 </div> |
| 301 | 301 |
| 302 <div class="slide"> | 302 <div class="slide"> |
| 303 <h2>Variables</h2> | 303 <h2>Variables</h2> |
| 304 <p> | 304 <p> |
| 305 The <code>var</code> statement declares a list of variables; | 305 The <code>var</code> statement declares a list of variables; |
| 306 as in function argument lists, the type is last. | 306 as in function argument lists, the type is last. |
| 307 | 307 |
| 308 When you run the example, notice that the <code>int</code>s are | |
| 309 <code>0</code> and the <code>bool</code>s are <code>false</code>. In | |
| 310 Go, uninitialized variables are defined to have the zero value for that | |
| adg 2012/02/28 01:53:22 Replace this sentence with: A variable declared w | |
| 311 type. | |
| 308 <div> | 312 <div> |
| 309 package main | 313 package main |
| 310 | 314 |
| 311 import "fmt" | 315 import "fmt" |
| 312 | 316 |
| 313 var x, y, z int | 317 var x, y, z int |
| 314 var c, python, java bool | 318 var c, python, java bool |
| 315 | 319 |
| 316 func main() { | 320 func main() { |
| 317 fmt.Println(x, y, z, c, python, java) | 321 fmt.Println(x, y, z, c, python, java) |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 609 <p> | 613 <p> |
| 610 Go's basic types are | 614 Go's basic types are |
| 611 <pre> | 615 <pre> |
| 612 bool | 616 bool |
| 613 | 617 |
| 614 string | 618 string |
| 615 | 619 |
| 616 int int8 int16 int32 int64 | 620 int int8 int16 int32 int64 |
| 617 uint uint8 uint16 uint32 uint64 uintptr | 621 uint uint8 uint16 uint32 uint64 uintptr |
| 618 | 622 |
| 619 rune // Represents a Unicode code point | 623 byte // Alias of uint8 |
| 624 rune // Represents a Unicode code point (alias of int32) | |
| 620 | 625 |
| 621 float32 float64 | 626 float32 float64 |
| 622 | 627 |
| 623 complex64 complex128 | 628 complex64 complex128 |
| 624 </pre> | 629 </pre> |
| 625 <div> | 630 <div> |
| 626 package main | 631 package main |
| 627 | 632 |
| 628 import ( | 633 import ( |
| 629 "math/cmplx" | 634 "math/cmplx" |
| (...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1375 } | 1380 } |
| 1376 </div> | 1381 </div> |
| 1377 </div> | 1382 </div> |
| 1378 | 1383 |
| 1379 <div class="slide"> | 1384 <div class="slide"> |
| 1380 <h2>Methods</h2> | 1385 <h2>Methods</h2> |
| 1381 <p> | 1386 <p> |
| 1382 In fact, you can define a method on <i>any</i> type you define in your | 1387 In fact, you can define a method on <i>any</i> type you define in your |
| 1383 package, not just structs. | 1388 package, not just structs. |
| 1384 <p> | 1389 <p> |
| 1385 » You cannot define a method on a type from another package, or on a | 1390 You cannot define a method on a basic type, an interface type, |
| adg 2012/02/28 01:53:22 tabs please | |
| 1386 » basic type. | 1391 or a type from another package. |
| 1387 <div> | 1392 <div> |
| 1388 package main | 1393 package main |
| 1389 | 1394 |
| 1390 import ( | 1395 import ( |
| 1391 "fmt" | 1396 "fmt" |
| 1392 "math" | 1397 "math" |
| 1393 ) | 1398 ) |
| 1394 | 1399 |
| 1395 type MyFloat float64 | 1400 type MyFloat float64 |
| 1396 | 1401 |
| (...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1863 <div class="slide"> | 1868 <div class="slide"> |
| 1864 <h2>Goroutines</h2> | 1869 <h2>Goroutines</h2> |
| 1865 <p> | 1870 <p> |
| 1866 A <i>goroutine</i> is a lightweight thread managed by the Go runtime. | 1871 A <i>goroutine</i> is a lightweight thread managed by the Go runtime. |
| 1867 <pre>go f(x, y, z)</pre> | 1872 <pre>go f(x, y, z)</pre> |
| 1868 <p> | 1873 <p> |
| 1869 starts a new goroutine running | 1874 starts a new goroutine running |
| 1870 <pre>f(x, y, z)</pre> | 1875 <pre>f(x, y, z)</pre> |
| 1871 <p> | 1876 <p> |
| 1872 The evaluation | 1877 The evaluation |
| 1873 of <code>f</code>, <code>x</code>, <code>y</code>, and <code>z</code> | 1878 » of <code>f</code>, <code>x</code>, <code>y</code>, and <code>z</code> |
| 1874 happens in the current goroutine and the execution of <code>f</code> | 1879 » happens in the current goroutine and the execution of <code>f</code> |
| 1875 happens in the new goroutine. | 1880 happens in the new goroutine. |
| 1876 <p> | 1881 <p> |
| 1877 Goroutines run in the same address space, so access to shared memory | 1882 Goroutines run in the same address space, so access to shared memory |
| 1878 must be synchronized. The <code><a href="http://golang.org/pkg/sync/" | 1883 must be synchronized. The <code><a href="http://golang.org/pkg/sync/" |
| 1879 target="_blank">sync</a></code> package provides useful primitives, | 1884 target="_blank">sync</a></code> package provides useful primitives, |
| 1880 although you won't need them much in Go as there are other primitives. | 1885 although you won't need them much in Go as there are other primitives. |
| 1881 (See the next slide.) | 1886 (See the next slide.) |
| 1882 <div> | 1887 <div> |
| 1883 package main | 1888 package main |
| 1884 | 1889 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2006 told there are no more values coming, such as to terminate a <code>range </code> | 2011 told there are no more values coming, such as to terminate a <code>range </code> |
| 2007 loop. | 2012 loop. |
| 2008 <div> | 2013 <div> |
| 2009 package main | 2014 package main |
| 2010 | 2015 |
| 2011 import ( | 2016 import ( |
| 2012 "fmt" | 2017 "fmt" |
| 2013 ) | 2018 ) |
| 2014 | 2019 |
| 2015 func fibonacci(n int, c chan int) { | 2020 func fibonacci(n int, c chan int) { |
| 2016 x, y := 1, 1 | 2021 x, y := 0, 1 |
| 2017 for i := 0; i < n; i++ { | 2022 for i := 0; i < n; i++ { |
| 2018 c <- x | 2023 c <- x |
| 2019 x, y = y, x + y | 2024 x, y = y, x + y |
| 2020 } | 2025 } |
| 2021 close(c) | 2026 close(c) |
| 2022 } | 2027 } |
| 2023 | 2028 |
| 2024 func main() { | 2029 func main() { |
| 2025 c := make(chan int, 10) | 2030 c := make(chan int, 10) |
| 2026 » go fibonacci(cap(c), c) | 2031 go fibonacci(cap(c), c) |
| adg 2012/02/28 01:53:22 did this change to spaces? why? Kyle E. Lemons 2012/02/28 02:14:24 Two of the examples down here use spaces. Not sur | |
| 2027 for i := range c { | 2032 for i := range c { |
| 2028 fmt.Println(i) | 2033 fmt.Println(i) |
| 2029 } | 2034 } |
| 2030 } | 2035 } |
| 2031 </div> | 2036 </div> |
| 2032 </div> | 2037 </div> |
| 2033 | 2038 |
| 2034 <div class="slide"> | 2039 <div class="slide"> |
| 2035 <h2>Select</h2> | 2040 <h2>Select</h2> |
| 2036 <p> | 2041 <p> |
| 2037 The <code>select</code> statement lets a goroutine wait on multiple | 2042 The <code>select</code> statement lets a goroutine wait on multiple |
| 2038 communication operations. | 2043 communication operations. |
| 2039 <p> | 2044 <p> |
| 2040 A <code>select</code> blocks until one of its cases can run, then it | 2045 A <code>select</code> blocks until one of its cases can run, then it |
| 2041 executes that case. It chooses one at random if multiple are ready. | 2046 executes that case. It chooses one at random if multiple are ready. |
| 2042 <div> | 2047 <div> |
| 2043 package main | 2048 package main |
| 2044 | 2049 |
| 2045 import "fmt" | 2050 import "fmt" |
| 2046 | 2051 |
| 2047 func fibonacci(c, quit chan int) { | 2052 func fibonacci(c, quit chan int) { |
| 2048 x, y := 1, 1 | 2053 x, y := 0, 1 |
| 2049 for { | 2054 for { |
| 2050 select { | 2055 select { |
| 2051 case c <- x: | 2056 case c <- x: |
| 2052 x, y = y, x + y | 2057 x, y = y, x + y |
| 2053 case <-quit: | 2058 case <-quit: |
| 2054 fmt.Println("quit") | 2059 fmt.Println("quit") |
| 2055 return | 2060 return |
| 2056 } | 2061 } |
| 2057 } | 2062 } |
| 2058 } | 2063 } |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2328 large archive of informative Go articles. | 2333 large archive of informative Go articles. |
| 2329 <p> | 2334 <p> |
| 2330 Visit <a target="_blank" href="http://golang.org">golang.org</a> for | 2335 Visit <a target="_blank" href="http://golang.org">golang.org</a> for |
| 2331 more. | 2336 more. |
| 2332 </div> | 2337 </div> |
| 2333 | 2338 |
| 2334 </div><!-- end slides --> | 2339 </div><!-- end slides --> |
| 2335 | 2340 |
| 2336 </body> | 2341 </body> |
| 2337 </html> | 2342 </html> |
| OLD | NEW |