File size: 81,499 Bytes
08c8a6d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 |
About This Book
1
License
The Little Go Book is licensed under the AttributionNonCommercialShareAlike 4.0 International license. You should
not have paid for this book.
You are free to copy, distribute, modify or display the book. However, I ask that you always attribute the book to me,
Karl Seguin, and do not use it for commercial purposes.
You can see the full text of the license at:
https://creativecommons.org/licenses/byncsa/4.0/
2
Latest Version
The latest source of this book is available at: https://github.com/karlseguin/thelittlegobook
3
4
Introduction
I’ve always had a lovehate relationship when it comes to learning new languages. On the one hand, languages are so
fundamental to what we do, that even small changes can have measurable impact. That aha moment when something
clicks can have a lasting effect on how you program and can redefine your expectations of other languages. On the
downside, language design is fairly incremental. Learning new keywords, type system, coding style as well as new
libraries, communities and paradigms is a lot of work that seems hard to justify. Compared to everything else we have
to learn, new languages often feel like a poor investment of our time.
That said, we have to move forward. We have to be willing to take incremental steps because, again, languages are the
foundation of what we do. Though the changes are often incremental, they tend to have a wide scope and they impact
productivity, readability, performance, testability, dependency management, error handling, documentation, profiling,
communities, standard libraries, and so on. Is there a positive way to say death by a thousand cuts?
That leaves us with an important question: why Go? For me, there are two compelling reasons. The first is that it’s
a relatively simple language with a relatively simple standard library. In a lot of ways, the incremental nature of Go is
to simplify some of the complexity we’ve seen being added to languages over the last couple of decades. The other
reason is that for many developers, it will complement your existing arsenal.
Go was built as a system language (e.g., operating systems, device drivers) and thus aimed at C and C++ developers.
According to the Go team, and which is certainly true of me, application developers, not system developers, have
become the primary Go users. Why? I can’t speak authoritatively for system developers, but for those of us building
websites, services, desktop applications and the like, it partially comes down to the emerging need for a class of systems
that sit somewhere in between lowlevel system applications and higherlevel applications.
Maybe it’s a messaging, caching, computationalheavy data analysis, command line interface, logging or monitoring.
I don’t know what label to give it, but over the course of my career, as systems continue to grow in complexity
and as concurrency frequently measures in the tens of thousands, there’s clearly been a growing need for custom
infrastructuretype systems. You can build such systems with Ruby or Python or something else (and many people do),
but these types of systems can benefit from a more rigid type system and greater performance. Similarly, you can use
Go to build websites (and many people do), but I still prefer, by a wide margin, the expressiveness of Node or Ruby for
such systems.
There are other areas where Go excels. For example, there are no dependencies when running a compiled Go program.
You don’t have to worry if your users have Ruby or the JVM installed, and if so, what version. For this reason, Go is
becoming increasingly popular as a language for commandline interface programs and other types of utility programs
5
you need to distribute (e.g., a log collector).
Put plainly, learning Go is an efficient use of your time. You won’t have to spend long hours learning or even mastering
Go, and you’ll end up with something practical from your effort.
6
A Note from the Author
I’ve hesitated writing this book for a couple reasons. The first is that Go’s own documentation, in particular Effective
Go, is solid.
The other is my discomfort at writing a book about a language. When I wrote The Little MongoDB Book, it was safe
to assume most readers understood the basics of relational database and modeling. With The Little Redis Book, you
could assume a familiarity with a key value store and take it from there.
As I think about the paragraphs and chapters that lay ahead, I know that I won’t be able to make those same assump
tions. How much time do you spend talking about interfaces knowing that for some, the concept will be new, while
others won’t need much more than Go has interfaces? Ultimately, I take comfort in knowing that you’ll let me know if
some parts are too shallow or others too detailed. Consider that the price of this book.
7
8
Getting Started
If you’re looking to play a little with Go, you should check out the Go Playground which lets you run code online without
having to install anything. This is also the most common way to share Go code when seeking help in Go’s discussion
forum and places like StackOverflow.
Installing Go is straightforward. You can install it from source, but I suggest you use one of the precompiled binaries.
When you go to the download page, you’ll see installers for various platforms. Let’s avoid these and learn how to set
up Go ourselves. As you’ll see, it isn’t hard.
Except for simple examples, Go is designed to work when your code is inside a workspace. The workspace is a folder
composed of bin, pkg and src subfolders. You might be tempted to force Go to follow your own style don’t.
Normally, I put my projects inside of ~/code. For example, ~/code/blog contains my blog. For Go, my workspace is
~/code/go and my Gopowered blog would be in ~/code/go/src/blog.
In short, create a go folder with a src subfolder wherever you expect to put your projects.
9
OSX / Linux
Download the tar.gz for your platform. For OSX, you’ll most likely be interested in go#.#.#.darwinamd64osx10
.8.tar.gz, where #.#.# is the latest version of Go.
Extract the file to /usr/local via tar C /usr/local xzf go#.#.#.darwinamd64osx10.8.tar.gz.
Set up two environment variables:
1. GOPATH points to your workspace, for me, that’s $HOME/code/go.
2. We need to append Go’s binary to our PATH.
You can set these up from a shell:
echo 'export GOPATH=$HOME/code/go' >> $HOME/.profile
echo 'export PATH=$PATH:/usr/local/go/bin' >> $HOME/.profile
You’ll want to activate these variables. You can close and reopen your shell, or you can run source $HOME/.profile.
Type go version and you’ll hopefully get an output that looks like go version go1.3.3 darwin/amd64.
10
Windows
Download the latest zip file. If you’re on an x64 system, you’ll want go#.#.#.windowsamd64.zip, where #.#.# is
the latest version of Go.
Unzip it at a location of your choosing. c:\Go is a good choice.
Set up two environment variables:
1. GOPATH points to your workspace. That might be something like c:\users\goku\work\go.
2. Add c:\Go\bin to your PATH environment variable.
Environment variables can be set through the Environment Variables button on the Advanced tab of the System
control panel. Some versions of Windows provide this control panel through the Advanced System Settings option
inside the System control panel.
Open a command prompt and type go version. You’ll hopefully get an output that looks like go version go1.3.3
windows/amd64.
11
12
Chapter 1 The Basics
Go is a compiled, statically typed language with a Clike syntax and garbage collection. What does that mean?
13
Compilation
Compilation is the process of translating the source code that you write into a lower level language – either assembly
(as is the case with Go), or some other intermediary language (as with Java and C#).
Compiled languages can be unpleasant to work with because compilation can be slow. It’s hard to iterate quickly if you
have to spend minutes or hours waiting for code to compile. Compilation speed is one of the major design goals of Go.
This is good news for people working on large projects as well as those of us used to a quick feedback cycle offered by
interpreted languages.
Compiled languages tend to run faster and the executable can be run without additional dependencies (at least, that’s
true for languages like C, C++ and Go which compile directly to assembly).
14
Static Typing
Being statically typed means that variables must be of a specific type (int, string, bool, []byte, etc.). This is either
achieved by specifying the type when the variable is declared or, in many cases, letting the compiler infer the type
(we’ll look at examples shortly).
There’s a lot more that can be said about static typing, but I believe it’s something better understood by looking at
code. If you’re used to dynamically typed languages, you might find this cumbersome. You’re not wrong, but there
are advantages, especially when you pair static typing with compilation. The two are often conflated. It’s true that
when you have one, you normally have the other but it isn’t a hard rule. With a rigid type system, a compiler is able
to detect problems beyond mere syntactical mistakes as well as make further optimizations.
15
CLike Syntax
Saying that a language has a Clike syntax means that if you’re used to any other Clike languages such as C, C++,
Java, JavaScript and C#, then you’re going to find Go familiar – superficially, at least. For example, it means && is used
as a boolean AND, == is used to compare equality, { and } start and end a scope, and array indexes start at 0.
Clike syntax also tends to mean semicolon terminated lines and parentheses around conditions. Go does away with
both of these, though parentheses are still used to control precedence. For example, an if statement looks like this:
if name == "Leto" {
print("the spice must flow")
}
And in more complicated cases, parentheses are still useful:
if (name == "Goku" && power > 9000) || (name == "gohan" && power < 4000) {
print("super Saiyan")
}
Beyond this, Go is much closer to C than C# or Java not only in terms of syntax, but in terms of purpose. That’s
reflected in the terseness and simplicity of the language which will hopefully start to become obvious as you learn it.
16
Garbage Collected
Some variables, when created, have an easytodefine life. A variable local to a function, for example, disappears when
the function exits. In other cases, it isn’t so obvious – at least to a compiler. For example, the lifetime of a variable
returned by a function or referenced by other variables and objects can be tricky to determine. Without garbage
collection, it’s up to developers to free the memory associated with such variables at a point where the developer
knows the variable isn’t needed. How? In C, you’d literally free(str); the variable.
Languages with garbage collectors (e.g., Ruby, Python, Java, JavaScript, C#, Go) are able to keep track of these
and free them when they’re no longer used. Garbage collection adds overhead, but it also eliminates a number of
devastating bugs.
17
Running Go Code
Let’s start our journey by creating a simple program and learning how to compile and execute it. Open your favorite
text editor and write the following code:
package main
func main() {
println("it's over 9000!")
}
Save the file as main.go. For now, you can save it anywhere you want; we don’t need to live inside Go’s workspace
for trivial examples.
Next, open a shell/command prompt and change the directory to where you saved the file. For me, that means typing
cd ~/code.
Finally, run the program by entering:
go run main.go
If everything worked, you should see it’s over 9000!.
But wait, what about the compilation step? go run is a handy command that compiles and runs your code. It uses
a temporary directory to build the program, executes it and then cleans itself up. You can see the location of the
temporary file by running:
go run work main.go
To explicitly compile code, use go build:
go build main.go
This will generate an executable main which you can run. On Linux / OSX, don’t forget that you need to prefix the
executable with dotslash, so you need to type ./main.
While developing, you can use either go run or go build. When you deploy your code however, you’ll want to deploy
a binary via go build and execute that.
Main
Hopefully, the code that we just executed is understandable. We’ve created a function and printed out a string with the
builtin println function. Did go run know what to execute because there was only a single choice? No. In Go, the
entry point to a program has to be a function called main within a package main.
We’ll talk more about packages in a later chapter. For now, while we focus on understanding the basics of Go, we’ll
always write our code within the main package.
If you want, you can alter the code and change the package name. Run the code via go run and you should get
an error. Then, change the name back to main but use a different function name. You should see a different error
18
message. Try making those same changes but use go build instead. Notice that the code compiles, there’s just no
entry point to run it. This is perfectly normal when you are, for example, building a library.
19
Imports
Go has a number of builtin functions, such as println, which can be used without reference. We can’t get very
far though, without making use of Go’s standard library and eventually using thirdparty libraries. In Go, the import
keyword is used to declare the packages that are used by the code in the file.
Let’s change our program:
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) != 2 {
os.Exit(1)
}
fmt.Println("It's over", os.Args[1])
}
Which you can run via:
go run main.go 9000
We’re now using two of Go’s standard packages: fmt and os. We’ve also introduced another builtin function len. len
returns the size of a string, or the number of values in a dictionary, or, as we see here, the number of elements in an
array. If you’re wondering why we expect 2 arguments, it’s because the first argument – at index 0 – is always the
path of the currently running executable. (Change the program to print it out and see for yourself.)
You’ve probably noticed we prefix the function name with the package, e.g., fmt.Println. This is different from many
other languages. We’ll learn more about packages in later chapters. For now, knowing how to import and use a package
is a good start.
Go is strict about importing packages. It will not compile if you import a package but don’t use it. Try to run the
following:
package main
import (
"fmt"
"os"
)
func main() {
}
20
You should get two errors about fmt and os being imported and not used. Can this get annoying? Absolutely. Over
time, you’ll get used to it (it’ll still be annoying though). Go is strict about this because unused imports can slow
compilation; admittedly a problem most of us don’t have to this degree.
Another thing to note is that Go’s standard library is well documented. You can head over to https://golang.org/pkg/
fmt/#Println to learn more about the Println function that we used. You can click on that section header and see the
source code. Also, scroll to the top to learn more about Go’s formatting capabilities.
If you’re ever stuck without internet access, you can get the documentation running locally via:
godoc http=:6060
and pointing your browser to http://localhost:6060
21
Variables and Declarations
It’d be nice to begin and end our look at variables by saying you declare and assign to a variable by doing x = 4.
Unfortunately, things are more complicated in Go. We’ll begin our conversation by looking at simple examples. Then,
in the next chapter, we’ll expand this when we look at creating and using structures. Still, it’ll probably take some time
before you truly feel comfortable with it.
You might be thinking Woah! What can be so complicated about this? Let’s start looking at some examples.
The most explicit way to deal with variable declaration and assignment in Go is also the most verbose:
package main
import (
"fmt"
)
func main() {
var power int
power = 9000
fmt.Printf("It's over %d\n", power)
}
Here, we declare a variable power of type int. By default, Go assigns a zero value to variables. Integers are assigned
0, booleans false, strings "" and so on. Next, we assign 9000 to our power variable. We can merge the first two
lines:
var power int = 9000
Still, that’s a lot of typing. Go has a handy short variable declaration operator, :=, which can infer the type:
power := 9000
This is handy, and it works just as well with functions:
func main() {
power := getPower()
}
func getPower() int {
return 9001
}
It’s important that you remember that := is used to declare the variable as well as assign a value to it. Why? Because
a variable can’t be declared twice (not in the same scope anyway). If you try to run the following, you’ll get an error.
func main() {
power := 9000
22
fmt.Printf("It's over %d\n", power)
// COMPILER ERROR:
// no new variables on left side of :=
power := 9001
fmt.Printf("It's also over %d\n", power)
}
The compiler will complain with no new variables on left side of :=. This means that when we first declare a variable,
we use := but on subsequent assignment, we use the assignment operator =. This makes a lot of sense, but it can be
tricky for your muscle memory to remember when to switch between the two.
If you read the error message closely, you’ll notice that variables is plural. That’s because Go lets you assign multiple
variables (using either = or :=):
func main() {
name, power := "Goku", 9000
fmt.Printf("%s's power is over %d\n", name, power)
}
As long as one of the variables is new, := can be used. Consider:
func main() {
power := 1000
fmt.Printf("default power is %d\n", power)
name, power := "Goku", 9000
fmt.Printf("%s's power is over %d\n", name, power)
}
Although power is being used twice with :=, the compiler won’t complain the second time we use it, it’ll see that the
other variable, name, is a new variable and allow :=. However, you can’t change the type of power. It was declared
(implicitly) as an integer and thus, can only be assigned integers.
For now, the last thing to know is that, like imports, Go won’t let you have unused variables. For example,
func main() {
name, power := "Goku", 1000
fmt.Printf("default power is %d\n", power)
}
won’t compile because name is declared but not used. Like unused imports it’ll cause some frustration, but overall I
think it helps with code cleanliness and readability.
There’s more to learn about declaration and assignments. For now, remember that you’ll use var NAME TYPE when
declaring a variable to its zero value, NAME := VALUE when declaring and assigning a value, and NAME = VALUE when
assigning to a previously declared variable.
23
Function Declarations
This is a good time to point out that functions can return multiple values. Let’s look at three functions: one with no
return value, one with one return value, and one with two return values.
func log(message string) {
}
func add(a int, b int) int {
}
func power(name string) (int, bool) {
}
We’d use the last one like so:
value, exists := power("goku")
if exists == false {
// handle this error case
}
Sometimes, you only care about one of the return values. In these cases, you assign the other values to _:
_, exists := power("goku")
if exists == false {
// handle this error case
}
This is more than a convention. _, the blank identifier, is special in that the return value isn’t actually assigned. This
lets you use _ over and over again regardless of the returned type.
Finally, there’s something else that you’re likely to run into with function declarations. If parameters share the same
type, we can use a shorter syntax:
func add(a, b int) int {
}
Being able to return multiple values is something you’ll use often. You’ll also frequently use _ to discard a value. Named
return values and the slightly less verbose parameter declaration aren’t that common. Still, you’ll run into all of these
sooner than later so it’s important to know about them.
24
Before You Continue
We looked at a number of small individual pieces and it probably feels disjointed at this point. We’ll slowly build larger
examples and hopefully, the pieces will start to come together.
If you’re coming from a dynamic language, the complexity around types and declarations might seem like a step
backwards. I don’t disagree with you. For some systems, dynamic languages are categorically more productive.
If you’re coming from a statically typed language, you’re probably feeling comfortable with Go. Inferred types and
multiple return values are nice (though certainly not exclusive to Go). Hopefully as we learn more, you’ll appreciate
the clean and terse syntax.
25
26
Chapter 2 Structures
Go isn’t an objectoriented (OO) language like C++, Java, Ruby and C#. It doesn’t have objects nor inheritance and
thus, doesn’t have the many concepts associated with OO such as polymorphism and overloading.
What Go does have are structures, which can be associated with methods. Go also supports a simple but effective form
of composition. Overall, it results in simpler code, but there’ll be occasions where you’ll miss some of what OO has to
offer. (It’s worth pointing out that composition over inheritance is an old battle cry and Go is the first language I’ve
used that takes a firm stand on the issue.)
Although Go doesn’t do OO like you may be used to, you’ll notice a lot of similarities between the definition of a structure
and that of a class. A simple example is the following Saiyan structure:
type Saiyan struct {
Name string
Power int
}
We’ll soon see how to add a method to this structure, much like you’d have methods as part of a class. Before we do
that, we have to dive back into declarations.
27
Declarations and Initializations
When we first looked at variables and declarations, we looked only at builtin types, like integers and strings. Now that
we’re talking about structures, we need to expand that conversation to include pointers.
The simplest way to create a value of our structure is:
goku := Saiyan{
Name: "Goku",
Power: 9000,
}
Note: The trailing , in the above structure is required. Without it, the compiler will give an error. You’ll appreciate the
required consistency, especially if you’ve used a language or format that enforces the opposite.
We don’t have to set all or even any of the fields. Both of these are valid:
goku := Saiyan{}
// or
goku := Saiyan{Name: "Goku"}
goku.Power = 9000
Just like unassigned variables have a zero value, so do fields.
Furthermore, you can skip the field name and rely on the order of the field declarations (though for the sake of clarity,
you should only do this for structures with few fields):
goku := Saiyan{"Goku", 9000}
What all of the above examples do is declare a variable goku and assign a value to it.
Many times though, we don’t want a variable that is directly associated with our value but rather a variable that has a
pointer to our value. A pointer is a memory address; it’s the location of where to find the actual value. It’s a level of
indirection. Loosely, it’s the difference between being at a house and having directions to the house.
Why do we want a pointer to the value, rather than the actual value? It comes down to the way Go passes arguments
to a function: as copies. Knowing this, what does the following print?
func main() {
goku := Saiyan{"Goku", 9000}
Super(goku)
fmt.Println(goku.Power)
}
func Super(s Saiyan) {
s.Power += 10000
}
28
The answer is 9000, not 19000. Why? Because Super made changes to a copy of our original goku value and thus,
changes made in Super weren’t reflected in the caller. To make this work as you probably expect, we need to pass a
pointer to our value:
func main() {
goku := &Saiyan{"Goku", 9000}
Super(goku)
fmt.Println(goku.Power)
}
func Super(s *Saiyan) {
s.Power += 10000
}
We made two changes. The first is the use of the & operator to get the address of our value (it’s called the address
of operator). Next, we changed the type of parameter Super expects. It used to expect a value of type Saiyan but
now expects an address of type *Saiyan, where *X means pointer to value of type X. There’s obviously some relation
between the types Saiyan and *Saiyan, but they are two distinct types.
Note that we’re still passing a copy of goku's value to Super it just so happens that goku's value has become an
address. That copy is the same address as the original, which is what that indirection buys us. Think of it as copying
the directions to a restaurant. What you have is a copy, but it still points to the same restaurant as the original.
We can prove that it’s a copy by trying to change where it points to (not something you’d likely want to actually do):
func main() {
goku := &Saiyan{"Goku", 9000}
Super(goku)
fmt.Println(goku.Power)
}
func Super(s *Saiyan) {
s = &Saiyan{"Gohan", 1000}
}
The above, once again, prints 9000. This is how many languages behave, including Ruby, Python, Java and C#. Go,
and to some degree C#, simply make the fact visible.
It should also be obvious that copying a pointer is going to be cheaper than copying a complex structure. On a 64bit
machine, a pointer is 64 bits large. If we have a structure with many fields, creating copies can be expensive. The
real value of pointers though is that they let you share values. Do we want Super to alter a copy of goku or alter the
shared goku value itself?
All this isn’t to say that you’ll always want a pointer. At the end of this chapter, after we’ve seen a bit more of what we
can do with structures, we’ll reexamine the pointerversusvalue question.
29
Functions on Structures
We can associate a method with a structure:
type Saiyan struct {
Name string
Power int
}
func (s *Saiyan) Super() {
s.Power += 10000
}
In the above code, we say that the type *Saiyan is the receiver of the Super method. We call Super like so:
goku := &Saiyan{"Goku", 9001}
goku.Super()
fmt.Println(goku.Power) // will print 19001
30
Constructors
Structures don’t have constructors. Instead, you create a function that returns an instance of the desired type (like a
factory):
func NewSaiyan(name string, power int) *Saiyan {
return &Saiyan{
Name: name,
Power: power,
}
}
This pattern rubs a lot of developers the wrong way. On the one hand, it’s a pretty slight syntactical change; on the
other, it does feel a little less compartmentalized.
Our factory doesn’t have to return a pointer; this is absolutely valid:
func NewSaiyan(name string, power int) Saiyan {
return Saiyan{
Name: name,
Power: power,
}
}
31
New
Despite the lack of constructors, Go does have a builtin new function which is used to allocate the memory required
by a type. The result of new(X) is the same as &X{}:
goku := new(Saiyan)
// same as
goku := &Saiyan{}
Which you use is up to you, but you’ll find that most people prefer the latter whenever they have fields to initialize,
since it tends to be easier to read:
goku := new(Saiyan)
goku.name = "goku"
goku.power = 9001
//vs
goku := &Saiyan {
name: "goku",
power: 9000,
}
Whichever approach you choose, if you follow the factory pattern above, you can shield the rest of your code from
knowing and worrying about any of the allocation details.
32
Fields of a Structure
In the example that we’ve seen so far, Saiyan has two fields Name and Power of types string and int, respectively.
Fields can be of any type – including other structures and types that we haven’t explored yet such as arrays, maps,
interfaces and functions.
For example, we could expand our definition of Saiyan:
type Saiyan struct {
Name string
Power int
Father *Saiyan
}
which we’d initialize via:
gohan := &Saiyan{
Name: "Gohan",
Power: 1000,
Father: &Saiyan {
Name: "Goku",
Power: 9001,
Father: nil,
},
}
33
Composition
Go supports composition, which is the act of including one structure into another. In some languages, this is called a
trait or a mixin. Languages that don’t have an explicit composition mechanism can always do it the long way. In Java,
there’s the possibility to extend structures with inheritance but, in a scenario where this is not an option, a mixin would
be written like this:
public class Person {
private String name;
public String getName() {
return this.name;
}
}
public class Saiyan {
// Saiyan is said to have a person
private Person person;
// we forward the call to person
public String getName() {
return this.person.getName();
}
...
}
This can get pretty tedious. Every method of Person needs to be duplicated in Saiyan. Go avoids this tediousness:
type Person struct {
Name string
}
func (p *Person) Introduce() {
fmt.Printf("Hi, I'm %s\n", p.Name)
}
type Saiyan struct {
*Person
Power int
}
// and to use it:
goku := &Saiyan{
Person: &Person{"Goku"},
Power: 9001,
34
}
goku.Introduce()
The Saiyan structure has a field of type *Person. Because we didn’t give it an explicit field name, we can implicitly
access the fields and functions of the composed type. However, the Go compiler did give it a field name, consider the
perfectly valid:
goku := &Saiyan{
Person: &Person{"Goku"},
}
fmt.Println(goku.Name)
fmt.Println(goku.Person.Name)
Both of the above will print “Goku”.
Is composition better than inheritance? Many people think that it’s a more robust way to share code. When using
inheritance, your class is tightly coupled to your superclass and you end up focusing on hierarchy rather than behavior.
Overloading
While overloading isn’t specific to structures, it’s worth addressing. Simply, Go doesn’t support overloading. For this
reason, you’ll see (and write) a lot of functions that look like Load, LoadById, LoadByName and so on.
However, because implicit composition is really just a compiler trick, we can “overwrite” the functions of a composed
type. For example, our Saiyan structure can have its own Introduce function:
func (s *Saiyan) Introduce() {
fmt.Printf("Hi, I'm %s. Ya!\n", s.Name)
}
The composed version is always available via s.Person.Introduce().
35
Pointers versus Values
As you write Go code, it’s natural to ask yourself should this be a value, or a pointer to a value? There are two pieces
of good news. First, the answer is the same regardless of which of the following we’re talking about:
• A local variable assignment
• Field in a structure
• Return value from a function
• Parameters to a function
• The receiver of a method
Secondly, if you aren’t sure, use a pointer.
As we already saw, passing values is a great way to make data immutable (changes that a function makes to it won’t
be reflected in the calling code). Sometimes, this is the behavior that you’ll want but more often, it won’t be.
Even if you don’t intend to change the data, consider the cost of creating a copy of large structures. Conversely, you
might have small structures, say:
type Point struct {
X int
Y int
}
In such cases, the cost of copying the structure is probably offset by being able to access X and Y directly, without any
indirection.
Again, these are all pretty subtle cases. Unless you’re iterating over thousands or possibly tens of thousands of such
points, you wouldn’t notice a difference.
36
Before You Continue
From a practical point of view, this chapter introduced structures, how to make an instance of a structure a receiver
of a function, and added pointers to our existing knowledge of Go’s type system. The following chapters will build on
what we know about structures as well as the inner workings that we’ve explored.
37
38
Chapter 3 Maps, Arrays and Slices
So far we’ve seen a number of simple types and structures. It’s now time to look at arrays, slices and maps.
39
Arrays
If you come from Python, Ruby, Perl, JavaScript or PHP (and more), you’re probably used to programming with dynamic
arrays. These are arrays that resize themselves as data is added to them. In Go, like many other languages, arrays
are fixed. Declaring an array requires that we specify the size, and once the size is specified, it cannot grow:
var scores [10]int
scores[0] = 339
The above array can hold up to 10 scores using indexes scores[0] through scores[9]. Attempts to access an out
of range index in the array will result in a compiler or runtime error.
We can initialize the array with values:
scores := [4]int{9001, 9333, 212, 33}
We can use len to get the length of the array. range can be used to iterate over it:
for index, value := range scores {
}
Arrays are efficient but rigid. We often don’t know the number of elements we’ll be dealing with upfront. For this, we
turn to slices.
40
Slices
In Go, you rarely, if ever, use arrays directly. Instead, you use slices. A slice is a lightweight structure that wraps and
represents a portion of an array. There are a few ways to create a slice, and we’ll go over when to use which later on.
The first is a slight variation on how we created an array:
scores := []int{1,4,293,4,9}
Unlike the array declaration, our slice isn’t declared with a length within the square brackets. To understand how the
two are different, let’s see another way to create a slice, using make:
scores := make([]int, 10)
We use make instead of new because there’s more to creating a slice than just allocating the memory (which is what
new does). Specifically, we have to allocate the memory for the underlying array and also initialize the slice. In the
above, we initialize a slice with a length of 10 and a capacity of 10. The length is the size of the slice, the capacity is
the size of the underlying array. Using make we can specify the two separately:
scores := make([]int, 0, 10)
This creates a slice with a length of 0 but with a capacity of 10. (If you’re paying attention, you’ll note that make and
len are overloaded. Go is a language that, to the frustration of some, makes use of features which aren’t exposed for
developers to use.)
To better understand the interplay between length and capacity, let’s look at some examples:
func main() {
scores := make([]int, 0, 10)
scores[7] = 9033
fmt.Println(scores)
}
Our first example crashes. Why? Because our slice has a length of 0. Yes, the underlying array has 10 elements, but
we need to explicitly expand our slice in order to access those elements. One way to expand a slice is via append:
func main() {
scores := make([]int, 0, 10)
scores = append(scores, 5)
fmt.Println(scores) // prints [5]
}
But that changes the intent of our original code. Appending to a slice of length 0 will set the first element. For whatever
reason, our crashing code wanted to set the element at index 7. To do this, we can reslice our slice:
func main() {
scores := make([]int, 0, 10)
scores = scores[0:8]
scores[7] = 9033
41
fmt.Println(scores)
}
How large can we resize a slice? Up to its capacity which, in this case, is 10. You might be thinking this doesn’t actually
solve the fixedlength issue of arrays. It turns out that append is pretty special. If the underlying array is full, it will
create a new larger array and copy the values over (this is exactly how dynamic arrays work in PHP, Python, Ruby,
JavaScript, …). This is why, in the example above that used append, we had to reassign the value returned by append
to our scores variable: append might have created a new value if the original had no more space.
If I told you that Go grew arrays with a 2x algorithm, can you guess what the following will output?
func main() {
scores := make([]int, 0, 5)
c := cap(scores)
fmt.Println(c)
for i := 0; i < 25; i++ {
scores = append(scores, i)
// if our capacity has changed,
// Go had to grow our array to accommodate the new data
if cap(scores) != c {
c = cap(scores)
fmt.Println(c)
}
}
}
The initial capacity of scores is 5. In order to hold 25 values, it’ll have to be expanded 3 times with a capacity of 10,
20 and finally 40.
As a final example, consider:
func main() {
scores := make([]int, 5)
scores = append(scores, 9332)
fmt.Println(scores)
}
Here, the output is going to be [0, 0, 0, 0, 0, 9332]. Maybe you thought it would be [9332, 0, 0, 0, 0]?
To a human, that might seem logical. To a compiler, you’re telling it to append a value to a slice that already holds 5
values.
Ultimately, there are four common ways to initialize a slice:
names := []string{"leto", "jessica", "paul"}
checks := make([]bool, 10)
42
var names []string
scores := make([]int, 0, 20)
When do you use which? The first one shouldn’t need much of an explanation. You use this when you know the values
that you want in the array ahead of time.
The second one is useful when you’ll be writing into specific indexes of a slice. For example:
func extractPowers(saiyans []*Saiyan) []int {
powers := make([]int, len(saiyans))
for index, saiyan := range saiyans {
powers[index] = saiyan.Power
}
return powers
}
The third version is a nil slice and is used in conjunction with append, when the number of elements is unknown.
The last version lets us specify an initial capacity; useful if we have a general idea of how many elements we’ll need.
Even when you know the size, append can be used. It’s largely a matter of preference:
func extractPowers(saiyans []*Saiyan) []int {
powers := make([]int, 0, len(saiyans))
for _, saiyan := range saiyans {
powers = append(powers, saiyan.Power)
}
return powers
}
Slices as wrappers to arrays is a powerful concept. Many languages have the concept of slicing an array. Both JavaScript
and Ruby arrays have a slice method. You can also get a slice in Ruby by using [START..END] or in Python via [
START:END]. However, in these languages, a slice is actually a new array with the values of the original copied over.
If we take Ruby, what’s the output of the following?
scores = [1,2,3,4,5]
slice = scores[2..4]
slice[0] = 999
puts scores
The answer is [1, 2, 3, 4, 5]. That’s because slice is a completely new array with copies of values. Now, consider
the Go equivalent:
scores := []int{1,2,3,4,5}
slice := scores[2:4]
slice[0] = 999
fmt.Println(scores)
43
The [X:Y] syntax creates a slice of scores, starting from index 2 up until (but not including) index 4. However, unlike
the Ruby example above, the Go code will produce an output of [1, 2, 999, 4, 5]. This is because our slice is
really just a window into scores.
This changes how you code. For example, a number of functions take a position parameter. In JavaScript, if we want
to find the first space in a string (yes, slices work on strings too!) after the first five characters, we’d write:
haystack = "the spice must flow";
console.log(haystack.indexOf(" ", 5));
In Go, we leverage slices:
strings.Index(haystack[5:], " ")
We can see from the above example, that [X:] is shorthand for from X to the end while [:X] is shorthand for from
the start up until X. Unlike other languages, Go doesn’t support negative values. If we want all of the values of a slice
except the last, we do:
scores := []int{1, 2, 3, 4, 5}
scores = scores[:len(scores)1]
The above is the start of an efficient way to remove a value from an unsorted slice:
func main() {
scores := []int{1, 2, 3, 4, 5}
scores = removeAtIndex(scores, 2)
fmt.Println(scores) // [1 2 5 4]
}
// won't preserve order
func removeAtIndex(source []int, index int) []int {
lastIndex := len(source) 1
//swap the last value and the value we want to remove
source[index], source[lastIndex] = source[lastIndex], source[index]
return source[:lastIndex]
}
Finally, now that we know about slices, we can look at another commonly used builtin function: copy. copy is one
of those functions that highlights how slices change the way we code. Normally, a method that copies values from
one array to another has 5 parameters: source, sourceStart, count, destination and destinationStart. With
slices, we only need two:
import (
"fmt"
"math/rand"
"sort"
)
44
func main() {
scores := make([]int, 100)
for i := 0; i < 100; i++ {
scores[i] = int(rand.Int31n(1000))
}
sort.Ints(scores)
worst := make([]int, 5)
copy(worst, scores[:5])
fmt.Println(worst)
}
Take some time and play with the above code. Try variations. See what happens if you change copy to something like
copy(worst[2:4], scores[:5]), or what if you try to copy more or less than 5 values into worst?
45
Maps
Maps in Go are what other languages call hashtables or dictionaries. They work as you expect: you define a key and
value, and can get, set and delete values from it.
Maps, like slices, are created with the make function. Let’s look at an example:
func main() {
lookup := make(map[string]int)
lookup["goku"] = 9001
power, exists := lookup["vegeta"]
// prints 0, false
// 0 is the default value for an integer
fmt.Println(power, exists)
}
To get the number of keys, we use len. To remove a value based on its key, we use delete:
// returns 1
total := len(lookup)
// has no return, can be called on a nonexisting key
delete(lookup, "goku")
Maps grow dynamically. However, we can supply a second argument to make to set an initial size:
lookup := make(map[string]int, 100)
If you have some idea of how many keys your map will have, defining an initial size can help with performance.
When you need a map as a field of a structure, you define it as:
type Saiyan struct {
Name string
Friends map[string]*Saiyan
}
One way to initialize the above is via:
goku := &Saiyan{
Name: "Goku",
Friends: make(map[string]*Saiyan),
}
goku.Friends["krillin"] = ... //todo load or create Krillin
There’s yet another way to declare and initialize values in Go. Like make, this approach is specific to maps and arrays.
We can declare as a composite literal:
46
lookup := map[string]int{
"goku": 9001,
"gohan": 2044,
}
We can iterate over a map using a for loop combined with the range keyword:
for key, value := range lookup {
...
}
Iteration over maps isn’t ordered. Each iteration over a lookup will return the key value pair in a random order.
47
Pointers versus Values
We finished Chapter 2 by looking at whether you should assign and pass pointers or values. We’ll now have this same
conversation with respect to array and map values. Which of these should you use?
a := make([]Saiyan, 10)
//or
b := make([]*Saiyan, 10)
Many developers think that passing b to, or returning it from, a function is going to be more efficient. However, what’s
being passed/returned is a copy of the slice, which itself is a reference. So with respect to passing/returning the slice
itself, there’s no difference.
Where you will see a difference is when you modify the values of a slice or map. At this point, the same logic that we
saw in Chapter 2 applies. So the decision on whether to define an array of pointers versus an array of values comes
down to how you use the individual values, not how you use the array or map itself.
48
Before You Continue
Arrays and maps in Go work much like they do in other languages. If you’re used to dynamic arrays, there might be
a small adjustment, but append should solve most of your discomfort. If we peek beyond the superficial syntax of
arrays, we find slices. Slices are powerful and they have a surprisingly large impact on the clarity of your code.
There are edge cases that we haven’t covered, but you’re not likely to run into them. And, if you do, hopefully the
foundation we’ve built here will let you understand what’s going on.
49
50
Chapter 4 Code Organization and Interfaces
It’s now time to look at how to organize our code.
51
Packages
To keep more complicated libraries and systems organized, we need to learn about packages. In Go, package names
follow the directory structure of your Go workspace. If we were building a shopping system, we’d probably start with
a package name “shopping” and put our source files in $GOPATH/src/shopping/.
We don’t want to put everything inside this folder though. For example, maybe we want to isolate some database logic
inside its own folder. To achieve this, we create a subfolder at $GOPATH/src/shopping/db. The package name of the
files within this subfolder is simply db, but to access it from another package, including the shopping package, we
need to import shopping/db.
In other words, when you name a package, via the package keyword, you provide a single value, not a complete
hierarchy (e.g., “shopping” or “db”). When you import a package, you specify the complete path.
Let’s try it. Inside your Go workspace’s src folder (which we set up in Getting Started of the Introduction), create a
new folder called shopping and a subfolder within it called db.
Inside of shopping/db, create a file called db.go and add the following code:
package db
type Item struct {
Price float64
}
func LoadItem(id int) *Item {
return &Item{
Price: 9.001,
}
}
Notice that the name of the package is the same as the name of the folder. Also, obviously, we aren’t actually accessing
the database. We’re just using this as an example to show how to organize code.
Now, create a file called pricecheck.go inside of the main shopping folder. Its content is:
package shopping
import (
"shopping/db"
)
func PriceCheck(itemId int) (float64, bool) {
item := db.LoadItem(itemId)
if item == nil {
return 0, false
}
return item.Price, true
52
}
It’s tempting to think that importing shopping/db is somehow special because we’re inside the shopping package/
folder already. In reality, you’re importing $GOPATH/src/shopping/db, which means you could just as easily import
test/db so long as you had a package named db inside of your workspace’s src/test folder.
If you’re building a package, you don’t need anything more than what we’ve seen. To build an executable, you still
need a main. The way I prefer to do this is to create a subfolder called main inside of shopping with a file called
main.go and the following content:
package main
import (
"shopping"
"fmt"
)
func main() {
fmt.Println(shopping.PriceCheck(4343))
}
You can now run your code by going into your shopping project and typing:
go run main/main.go
Cyclical Imports
As you start writing more complex systems, you’re bound to run into cyclical imports. This happens when package A
imports package B but package B imports package A (either directly or indirectly through another package). This is
something the compiler won’t allow.
Let’s change our shopping structure to cause the error.
Move the Item definition from shopping/db/db.go into shopping/pricecheck.go. Your pricecheck.go file
should now look like:
package shopping
import (
"shopping/db"
)
type Item struct {
Price float64
}
func PriceCheck(itemId int) (float64, bool) {
53
item := db.LoadItem(itemId)
if item == nil {
return 0, false
}
return item.Price, true
}
If you try to run the code, you’ll get a couple of errors from db/db.go about Item being undefined. This makes
sense. Item no longer exists in the db package; it’s been moved to the shopping package. We need to change
shopping/db/db.go to:
package db
import (
"shopping"
)
func LoadItem(id int) *shopping.Item {
return &shopping.Item{
Price: 9.001,
}
}
Now when you try to run the code, you’ll get a dreaded import cycle not allowed error. We solve this by introducing
another package which contains shared structures. Your directory structure should look like:
$GOPATH/src
shopping
pricecheck.go
db
db.go
models
item.go
main
main.go
pricecheck.go will still import shopping/db, but db.go will now import shopping/models instead of shopping,
thus breaking the cycle. Since we moved the shared Item structure to shopping/models/item.go, we need to
change shopping/db/db.go to reference the Item structure from models package:
package db
import (
"shopping/models"
)
54
func LoadItem(id int) *models.Item {
return &models.Item{
Price: 9.001,
}
}
You’ll often need to share more than just models, so you might have other similar folder named utilities and such.
The important rule about these shared packages is that they shouldn’t import anything from the shopping package or
any subpackages. In a few sections, we’ll look at interfaces which can help us untangle these types of dependencies.
Visibility
Go uses a simple rule to define what types and functions are visible outside of a package. If the name of the type or
function starts with an uppercase letter, it’s visible. If it starts with a lowercase letter, it isn’t.
This also applies to structure fields. If a structure field name starts with a lowercase letter, only code within the same
package will be able to access them.
For example, if our items.go file had a function that looked like:
func NewItem() *Item {
// ...
}
it could be called via models.NewItem(). But if the function was named newItem, we wouldn’t be able to access it
from a different package.
Go ahead and change the name of the various functions, types and fields from the shopping code. For example, if
you rename the Item's Price field to price, you should get an error.
Package Management
The go command we’ve been using to run and build has a get subcommand which is used to fetch thirdparty
libraries. go get supports various protocols but for this example, we’ll be getting a library from Github, meaning,
you’ll need git installed on your computer.
Assuming you already have git installed, from a shell/command prompt, enter:
go get github.com/mattn/gosqlite3
go get fetches the remote files and stores them in your workspace. Go ahead and check your $GOPATH/src. In
addition to the shopping project that we created, you’ll now see a github.com folder. Within, you’ll see a mattn
folder which contains a gosqlite3 folder.
We just talked about how to import packages that live in our workspace. To use our newly gotten gosqlite3 package,
we’d import it like so:
import (
"github.com/mattn/gosqlite3"
55
)
I know this looks like a URL but in reality, it’ll simply import the gosqlite3 package which it expects to find in
$GOPATH/src/github.com/mattn/gosqlite3.
Dependency Management
go get has a couple of other tricks up its sleeve. If we go get within a project, it’ll scan all the files, looking for
imports to thirdparty libraries and will download them.
In a way, our own source code becomes a Gemfile or
package.json.
If you call go get u it’ll update the packages (or you can update a specific package via go get u FULL_PACKAGE_NAME
).
Eventually, you might find go get inadequate. For one thing, there’s no way to specify a revision, it always points to
the master/head/trunk/default. This is an even larger problem if you have two projects needing different versions of
the same library.
To solve this, you can use a thirdparty dependency management tool. They are still young, but two promising ones
are goop and godep. A more complete list is available at the gowiki.
56
Interfaces
Interfaces are types that define a contract but not an implementation. Here’s an example:
type Logger interface {
Log(message string)
}
You might be wondering what purpose this could possibly serve. Interfaces help decouple your code from specific
implementations. For example, we might have various types of loggers:
type SqlLogger struct { ... }
type ConsoleLogger struct { ... }
type FileLogger struct { ... }
Yet by programming against the interface, rather than these concrete implementations, we can easily change (and
test) which we use without any impact to our code.
How would you use one? Just like any other type, it could be a structure’s field:
type Server struct {
logger Logger
}
or a function parameter (or return value):
func process(logger Logger) {
logger.Log("hello!")
}
In a language like C# or Java, we have to be explicit when a class implements an interface:
public class ConsoleLogger : Logger {
public void Logger(message string) {
Console.WriteLine(message)
}
}
In Go, this happens implicitly. If your structure has a function name Log with a string parameter and no return value,
then it can be used as a Logger. This cuts down on the verboseness of using interfaces:
type ConsoleLogger struct {}
func (l ConsoleLogger) Log(message string) {
fmt.Println(message)
}
It also tends to promote small and focused interfaces. The standard library is full of interfaces. The io package has
a handful of popular ones such as io.Reader, io.Writer, and io.Closer. If you write a function that expects a
57
parameter that you’ll only be calling Close() on, you absolutely should accept an io.Closer rather than whatever
concrete type you’re using.
Interfaces can also participate in composition. And, interfaces themselves can be composed of other interfaces. For
example, io.ReadCloser is an interface composed of the io.Reader interface as well as the io.Closer interface.
Finally, interfaces are commonly used to avoid cyclical imports. Since they don’t have implementations, they’ll have
limited dependencies.
58
Before You Continue
Ultimately, how you structure your code around Go’s workspace is something that you’ll only feel comfortable with after
you’ve written a couple of nontrivial projects. What’s most important for you to remember is the tight relationship
between package names and your directory structure (not just within a project, but within the entire workspace).
The way Go handles visibility of types is straightforward and effective. It’s also consistent. There are a few things we
haven’t looked at, such as constants and global variables but rest assured, their visibility is determined by the same
naming rule.
Finally, if you’re new to interfaces, it might take some time before you get a feel for them. However, the first time
you see a function that expects something like io.Reader, you’ll find yourself thanking the author for not demanding
more than he or she needed.
59
60
Chapter 5 Tidbits
In this chapter, we’ll talk about a miscellany of Go’s feature which didn’t quite fit anywhere else.
61
Error Handling
Go’s preferred way to deal with errors is through return values, not exceptions. Consider the strconv.Atoi function
which takes a string and tries to convert it to an integer:
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
if len(os.Args) != 2 {
os.Exit(1)
}
n, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("not a valid number")
} else {
fmt.Println(n)
}
}
You can create your own error type; the only requirement is that it fulfills the contract of the builtin error interface,
which is:
type error interface {
Error() string
}
More commonly, we can create our own errors by importing the errors package and using it in the New function:
import (
"errors"
)
func process(count int) error {
if count < 1 {
return errors.New("Invalid count")
}
...
return nil
62
}
There’s a common pattern in Go’s standard library of using error variables. For example, the io package has an EOF
variable which is defined as:
var EOF = errors.New("EOF")
This is a package variable (it’s defined outside of a function) which is publicly accessible (uppercase first letter). Various
functions can return this error, say when we’re reading from a file or STDIN. If it makes contextual sense, you should
use this error, too. As consumers, we can use this singleton:
package main
import (
"fmt"
"io"
)
func main() {
var input int
_, err := fmt.Scan(&input)
if err == io.EOF {
fmt.Println("no more input!")
}
}
As a final note, Go does have panic and recover functions. panic is like throwing an exception while recover is like
catch; they are rarely used.
63
Defer
Even though Go has a garbage collector, some resources require that we explicitly release them. For example, we need
to Close() files after we’re done with them. This sort of code is always dangerous. For one thing, as we’re writing
a function, it’s easy to forget to Close something that we declared 10 lines up. For another, a function might have
multiple return points. Go’s solution is the defer keyword:
package main
import (
"fmt"
"os"
)
func main() {
file, err := os.Open("a_file_to_read")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
// read the file
}
If you try to run the above code, you’ll probably get an error (the file doesn’t exist). The point is to show how defer
works. Whatever you defer will be executed after the enclosing function (in this case main()) returns, even if it does
so violently. This lets you release resources near where it’s initialized and takes care of multiple return points.
64
go fmt
Most programs written in Go follow the same formatting rules, namely, a tab is used to indent and braces go on the
same line as their statement.
I know, you have your own style and you want to stick to it. That’s what I did for a long time, but I’m glad I eventually
gave in. A big reason for this is the go fmt command. It’s easy to use and authoritative (so no one argues over
meaningless preferences).
When you’re inside a project, you can apply the formatting rule to it and all subprojects via:
go fmt ./...
Give it a try. It does more than indent your code; it also aligns field declarations and alphabetically orders imports.
65
Initialized If
Go supports a slightly modified ifstatement, one where a value can be initiated prior to the condition being evaluated:
if x := 10; count > x {
...
}
That’s a pretty silly example. More realistically, you might do something like:
if err := process(); err != nil {
return err
}
Interestingly, while the values aren’t available outside the ifstatement, they are available inside any else if or else.
66
Empty Interface and Conversions
In most objectoriented languages, a builtin base class, often named object, is the superclass for all other classes.
Go, having no inheritance, doesn’t have such a superclass. What it does have is an empty interface with no methods:
interface{}. Since every type implements all 0 of the empty interface’s methods, and since interfaces are implicitly
implemented, every type fulfills the contract of the empty interface.
If we wanted to, we could write an add function with the following signature:
func add(a interface{}, b interface{}) interface{} {
...
}
To convert an interface variable to an explicit type, you use .(TYPE):
return a.(int) + b.(int)
Note that if the underlying type is not int, the above will result in an error.
You also have access to a powerful type switch:
switch a.(type) {
case int:
fmt.Printf("a is now an int and equals %d\n", a)
case bool, string:
// ...
default:
// ...
}
You’ll see and probably use the empty interface more than you might first expect. Admittedly, it won’t result in clean
code. Converting values back and forth is ugly and dangerous but sometimes, in a static language, it’s the only choice.
67
Strings and Byte Arrays
Strings and byte arrays are closely related. We can easily convert one to the other:
stra := "the spice must flow"
byts := []byte(stra)
strb := string(byts)
In fact, this way of converting is common across various types as well. Some functions explicitly expect an int32 or
an int64 or their unsigned counterparts. You might find yourself having to do things like:
int64(count)
Still, when it comes to bytes and strings, it’s probably something you’ll end up doing often. Do note that when you use
[]byte(X) or string(X), you’re creating a copy of the data. This is necessary because strings are immutable.
Strings are made of runes which are unicode code points. If you take the length of a string, you might not get what
you expect. The following prints 3:
fmt.Println(len(""))
If you iterate over a string using range, you’ll get runes, not bytes. Of course, when you turn a string into a []byte
you’ll get the correct data.
68
Function Type
Functions are firstclass types:
type Add func(a int, b int) int
which can then be used anywhere – as a field type, as a parameter, as a return value.
package main
import (
"fmt"
)
type Add func(a int, b int) int
func main() {
fmt.Println(process(func(a int, b int) int{
return a + b
}))
}
func process(adder Add) int {
return adder(1, 2)
}
Using functions like this can help decouple code from specific implementations much like we achieve with interfaces.
69
Before You Continue
We looked at various aspects of programming with Go. Most notably, we saw how error handling behaves and how to
release resources such as connections and open files. Many people dislike Go’s approach to error handling. It can feel
like a step backwards. Sometimes, I agree. Yet, I also find that it results in code that’s easier to follow. defer is an
unusual but practical approach to resource management. In fact, it isn’t tied to resource management only. You can
use defer for any purpose, such as logging when a function exits.
Certainly, we haven’t looked at all of the tidbits Go has to offer. But you should be feeling comfortable enough to tackle
whatever you come across.
70
Chapter 6 Concurrency
Go is often described as a concurrentfriendly language. The reason for this is that it provides a simple syntax over
two powerful mechanisms: goroutines and channels.
71
Goroutines
A goroutine is similar to a thread, but it is scheduled by Go, not the OS. Code that runs in a goroutine can run
concurrently with other code. Let’s look at an example:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("start")
go process()
time.Sleep(time.Millisecond * 10) // this is bad, don't do this!
fmt.Println("done")
}
func process() {
fmt.Println("processing")
}
There are a few interesting things going on here, but the most important is how we start a goroutine. We simply use
the go keyword followed by the function we want to execute. If we just want to run a bit of code, such as the above,
we can use an anonymous function. Do note that anonymous functions aren’t only used with goroutines, however.
go func() {
fmt.Println("processing")
}()
Goroutines are easy to create and have little overhead. Multiple goroutines will end up running on the same underlying
OS thread. This is often called an M:N threading model because we have M application threads (goroutines) running
on N OS threads. The result is that a goroutine has a fraction of overhead (a few KB) than OS threads. On modern
hardware, it’s possible to have millions of goroutines.
Furthermore, the complexity of mapping and scheduling is hidden. We just say this code should run concurrently and
let Go worry about making it happen.
If we go back to our example, you’ll notice that we had to Sleep for a few milliseconds. That’s because the main
process exits before the goroutine gets a chance to execute (the process doesn’t wait until all goroutines are finished
before exiting). To solve this, we need to coordinate our code.
72
Synchronization
Creating goroutines is trivial, and they are so cheap that we can start many; however, concurrent code needs to be
coordinated. To help with this problem, Go provides channels. Before we look at channels, I think it’s important to
understand a little bit about the basics of concurrent programming.
Writing concurrent code requires that you pay specific attention to where and how you read and write values. In some
ways, it’s like programming without a garbage collector – it requires that you think about your data from a new angle,
always watchful for possible danger. Consider:
package main
import (
"fmt"
"time"
)
var counter = 0
func main() {
for i := 0; i < 20; i++ {
go incr()
}
time.Sleep(time.Millisecond * 10)
}
func incr() {
counter++
fmt.Println(counter)
}
What do you think the output will be?
If you think the output is 1, 2, ... 20 you’re both right and wrong. It’s true that if you run the above code, you’ll
sometimes get that output. However, the reality is that the behavior is undefined. Why? Because we potentially have
multiple (two in this case) goroutines writing to the same variable, counter, at the same time. Or, just as bad, one
goroutine would be reading counter while another writes to it.
Is that really a danger? Yes, absolutely. counter++ might seem like a simple line of code, but it actually gets broken
down into multiple assembly statements – the exact nature is dependent on the platform that you’re running. If you run
this example, you’ll see that very often the numbers are printed in a weird order, and/or numbers are duplicated/missing.
There are worse possibilities too, such as system crashes or accessing an arbitrary piece of data and incrementing it!
The only concurrent thing you can safely do to a variable is to read from it. You can have as many readers as you want,
but writes need to be synchronized. There are various ways to do this, including using some truly atomic operations
that rely on special CPU instructions. However, the most common approach is to use a mutex:
73
package main
import (
"fmt"
"time"
"sync"
)
var (
counter = 0
lock sync.Mutex
)
func main() {
for i := 0; i < 20; i++ {
go incr()
}
time.Sleep(time.Millisecond * 10)
}
func incr() {
lock.Lock()
defer lock.Unlock()
counter++
fmt.Println(counter)
}
A mutex serializes access to the code under lock. The reason we simply define our lock as lock sync.Mutex is because
the default value of a sync.Mutex is unlocked.
Seems simple enough? The example above is deceptive. There’s a whole class of serious bugs that can arise when
doing concurrent programming. First of all, it isn’t always so obvious what code needs to be protected. While it might
be tempting to use coarse locks (locks that cover a large amount of code), that undermines the very reason we’re doing
concurrent programming in the first place. We generally want fine locks; else, we end up with a tenlane highway that
suddenly turns into a onelane road.
The other problem has to do with deadlocks. With a single lock, this isn’t a problem, but if you’re using two or more
locks around the same code, it’s dangerously easy to have situations where goroutineA holds lockA but needs access
to lockB, while goroutineB holds lockB but needs access to lockA.
It actually is possible to deadlock with a single lock, if we forget to release it. This isn’t as dangerous as a multilock
deadlock (because those are really tough to spot), but just so you can see what happens, try running:
package main
import (
74
"time"
"sync"
)
var (
lock sync.Mutex
)
func main() {
go func() { lock.Lock() }()
time.Sleep(time.Millisecond * 10)
lock.Lock()
}
There’s more to concurrent programming than what we’ve seen so far. For one thing, there’s another common mutex
called a readwrite mutex. This exposes two locking functions: one to lock for reading and one to lock for writing.
This distinction allows multiple simultaneous readers while ensuring that writing is exclusive. In Go, sync.RWMutex
is such a lock. In addition to the Lock and Unlock methods of a sync.Mutex, it also exposes RLock and RUnlock
methods; where R stands for Read. While readwrite mutexes are commonly used, they place an additional burden on
developers: we must now pay attention to not only when we’re accessing data, but also how.
Furthermore, part of concurrent programming isn’t so much about serializing access across the narrowest possible piece
of code; it’s also about coordinating multiple goroutines. For example, sleeping for 10 milliseconds isn’t a particularly
elegant solution. What if a goroutine takes more than 10 milliseconds? What if it takes less and we’re just wasting
cycles? Also, what if instead of just waiting for goroutines to finish, we want to tell one hey, I have new data for you
to process!?
These are all things that are doable without channels. Certainly for simpler cases, I believe you should use primitives
such as sync.Mutex and sync.RWMutex, but as we’ll see in the next section, channels aim at making concurrent
programming cleaner and less errorprone.
75
Channels
The challenge with concurrent programming stems from sharing data. If your goroutines share no data, you needn’t
worry about synchronizing them. That isn’t an option for all systems, however. In fact, many systems are built with
the exact opposite goal in mind: to share data across multiple requests. An inmemory cache or a database, are good
examples of this. This is becoming an increasingly common reality.
Channels help make concurrent programming saner by taking shared data out of the picture. A channel is a commu
nication pipe between goroutines which is used to pass data. In other words, a goroutine that has data can pass it to
another goroutine via a channel. The result is that, at any point in time, only one goroutine has access to the data.
A channel, like everything else, has a type. This is the type of data that we’ll be passing through our channel. For
example, to create a channel which can be used to pass an integer around, we’d do:
c := make(chan int)
The type of this channel is chan int. Therefore, to pass this channel to a function, our signature looks like:
func worker(c chan int) { ... }
Channels support two operations: receiving and sending. We send to a channel by doing:
CHANNEL < DATA
and receive from one by doing
VAR := <CHANNEL
The arrow points in the direction that data flows. When sending, the data flows into the channel. When receiving, the
data flows out of the channel.
The final thing to know before we look at our first example is that receiving and sending to and from a channel is
blocking. That is, when we receive from a channel, execution of the goroutine won’t continue until data is available.
Similarly, when we send to a channel, execution won’t continue until the data is received.
Consider a system with incoming data that we want to handle in separate goroutines. This is a common requirement.
If we did our dataintensive processing on the goroutine which accepts the incoming data, we’d risk timing out clients.
First, we’ll write our worker. This could be a simple function, but I’ll make it part of a structure since we haven’t seen
goroutines used like this before:
type Worker struct {
id int
}
func (w Worker) process(c chan int) {
for {
data := <c
fmt.Printf("worker %d got %d\n", w.id, data)
}
}
76
Our worker is simple. It waits until data is available then “processes” it. Dutifully, it does this in a loop, forever waiting
for more data to process.
To use this, the first thing we’d do is start some workers:
c := make(chan int)
for i := 0; i < 5; i++ {
worker := &Worker{id: i}
go worker.process(c)
}
And then we can give them some work:
for {
c < rand.Int()
time.Sleep(time.Millisecond * 50)
}
Here’s the complete code to make it run:
package main
import (
"fmt"
"time"
"math/rand"
)
func main() {
c := make(chan int)
for i := 0; i < 5; i++ {
worker := &Worker{id: i}
go worker.process(c)
}
for {
c < rand.Int()
time.Sleep(time.Millisecond * 50)
}
}
type Worker struct {
id int
}
func (w *Worker) process(c chan int) {
77
for {
data := <c
fmt.Printf("worker %d got %d\n", w.id, data)
}
}
We don’t know which worker is going to get what data. What we do know, what Go guarantees, is that the data we
send to a channel will only be received by a single receiver.
Notice that the only shared state is the channel, which we can safely receive from and send to concurrently. Channels
provide all of the synchronization code we need and also ensure that, at any given time, only one goroutine has access
to a specific piece of data.
Buffered Channels
Given the above code, what happens if we have more data coming in than we can handle? You can simulate this by
changing the worker to sleep after it has received data:
for {
data := <c
fmt.Printf("worker %d got %d\n", w.id, data)
time.Sleep(time.Millisecond * 500)
}
What’s happening is that our main code, the one that accepts the user’s incoming data (which we just simulated with
a random number generator) is blocking as it sends to the channel because no receiver is available.
In cases where you need high guarantees that the data is being processed, you probably will want to start blocking the
client. In other cases, you might be willing to loosen those guarantees. There are a few popular strategies to do this.
The first is to buffer the data. If no worker is available, we want to temporarily store the data in some sort of queue.
Channels have this buffering capability builtin. When we created our channel with make, we can give our channel a
length:
c := make(chan int, 100)
You can make this change, but you’ll notice that the processing is still choppy. Buffered channels don’t add more
capacity; they merely provide a queue for pending work and a good way to deal with a sudden spike. In our example,
we’re continuously pushing more data than our workers can handle.
Nevertheless, we can get a sense what the buffered channel is, in fact, buffering by looking at the channel’s len:
for {
c < rand.Int()
fmt.Println(len(c))
time.Sleep(time.Millisecond * 50)
}
You can see that it grows and grows until it fills up, at which point sending to our channel start to block again.
78
Select
Even with buffering, there comes a point where we need to start dropping messages. We can’t use up an infinite
amount of memory hoping a worker frees up. For this, we use Go’s select.
Syntactically, select looks a bit like a switch. With it, we can provide code for when the channel isn’t available to send
to. First, let’s remove our channel’s buffering so that we can clearly see how select works:
c := make(chan int)
Next, we change our for loop:
for {
select {
case c < rand.Int():
//optional code here
default:
//this can be left empty to silently drop the data
fmt.Println("dropped")
}
time.Sleep(time.Millisecond * 50)
}
We’re pushing out 20 messages per second, but our workers can only handle 10 per second; thus, half the messages
get dropped.
This is only the start of what we can accomplish with select. A main purpose of select is to manage multiple channels.
Given multiple channels, select will block until the first one becomes available. If no channel is available, default is
executed if one is provided. A channel is randomly picked when multiple are available.
It’s hard to come up with a simple example that demonstrates this behavior as it’s a fairly advanced feature. The next
section might help illustrate this though.
Timeout
We’ve looked at buffering messages as well as simply dropping them. Another popular option is to timeout. We’re
willing to block for some time, but not forever. This is also something easy to achieve in Go. Admittedly, the syntax
might be hard to follow but it’s such a neat and useful feature that I couldn’t leave it out.
To block for a maximum amount of time, we can use the time.After function. Let’s look at it then try to peek beyond
the magic. To use this, our sender becomes:
for {
select {
case c < rand.Int():
case <time.After(time.Millisecond * 100):
fmt.Println("timed out")
}
79
time.Sleep(time.Millisecond * 50)
}
time.After returns a channel, so we can select from it. The channel is written to after the specified time expires.
That’s it. There’s nothing more magical than that. If you’re curious, here’s what an implementation of after could
look like:
func after(d time.Duration) chan bool {
c := make(chan bool)
go func() {
time.Sleep(d)
c < true
}()
return c
}
Back to our select, there are a couple of things to play with. First, what happens if you add the default case back?
Can you guess? Try it. If you aren’t sure what’s going on, remember that default fires immediately if no channel is
available.
Also, time.After is a channel of type chan time.Time. In the above example, we simply discard the value that was
sent to the channel. If you want though, you can receive it:
case t := <time.After(time.Millisecond * 100):
fmt.Println("timed out at", t)
Pay close attention to our select. Notice that we’re sending to c but receiving from time.After. select works the
same regardless of whether we’re receiving from, sending to, or any combination of channels:
• The first available channel is chosen.
• If multiple channels are available, one is randomly picked.
• If no channel is available, the default case is executed.
• If there’s no default, select blocks.
Finally, it’s common to see a select inside a for. Consider:
for {
select {
case data := <c:
fmt.Printf("worker %d got %d\n", w.id, data)
case <time.After(time.Millisecond * 10):
fmt.Println("Break time")
time.Sleep(time.Second)
}
}
80
Before You Continue
If you’re new to the world of concurrent programming, it might all seem rather overwhelming. It categorically demands
considerably more attention and care. Go aims to make it easier.
Goroutines effectively abstract what’s needed to run concurrent code. Channels help eliminate some serious bugs that
can happen when data is shared by eliminating the sharing of data. This doesn’t just eliminate bugs, but it changes
how one approaches concurrent programming. You start to think about concurrency with respect to message passing,
rather than dangerous areas of code.
Having said that, I still make extensive use of the various synchronization primitives found in the sync and sync/
atomic packages. I think it’s important to be comfortable with both. I encourage you to first focus on channels, but
when you see a simple example that needs a shortlived lock, consider using a mutex or readwrite mutex.
81
82
Conclusion
I recently heard Go described as a boring language. Boring because it’s easy to learn, easy to write and, most
importantly, easy to read. Perhaps, I did this reality a disservice. We did spend three chapters talking about types and
how to declare variables after all.
If you have a background in a statically typed language, much of what we saw was probably, at best, a refresher. That
Go makes pointers visible and that slices are thin wrappers around arrays probably isn’t overwhelming to seasoned
Java or C# developers.
If you’ve mostly been making use of dynamic languages, you might feel a little different. It is a fair bit to learn. Not
least of which is the various syntax around declaration and initialization. Despite being a fan of Go, I find that for all the
progress towards simplicity, there’s something less than simple about it. Still, it comes down to some basic rules (like
you can only declare variable once and := does declare the variable) and fundamental understanding (like new(X) or
&X{} only allocate memory, but slices, maps and channels require more initialization and thus, make).
Beyond this, Go gives us a simple but effective way to organize our code. Interfaces, returnbased error handling,
defer for resource management and a simple way to achieve composition.
Last but not least is the builtin support for concurrency. There’s little to say about goroutines other than they’re
effective and simple (simple to use anyway). It’s a good abstraction. Channels are more complicated. I always think
it’s important to understand basics before using highlevel wrappers. I do think learning about concurrent programming
without channels is useful. Still, channels are implemented in a way that, to me, doesn’t feel quite like a simple
abstraction. They are almost their own fundamental building block. I say this because they change how you write and
think about concurrent programming. Given how hard concurrent programming can be, that is definitely a good thing.
83
|