| OLD | NEW |
| 1 <!--{ | 1 <!--{ |
| 2 "Title": "Go For C++ Programmers" | 2 "Title": "Go For C++ Programmers" |
| 3 }--> | 3 }--> |
| 4 | 4 |
| 5 <p> | 5 <p> |
| 6 Go is a systems programming language intended to be a general-purpose | 6 Go is a systems programming language intended to be a general-purpose |
| 7 systems language, like C++. | 7 systems language, like C++. |
| 8 These are some notes on Go for experienced C++ programmers. This | 8 These are some notes on Go for experienced C++ programmers. This |
| 9 document discusses the differences between Go and C++, and says little | 9 document discusses the differences between Go and C++, and says little |
| 10 to nothing about the similarities. | 10 to nothing about the similarities. |
| (...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 To use <code>manager2</code>, given a channel to it: | 700 To use <code>manager2</code>, given a channel to it: |
| 701 | 701 |
| 702 <pre> | 702 <pre> |
| 703 func f4(ch <- chan cmd2) int { | 703 func f4(ch <- chan cmd2) int { |
| 704 myCh := make(chan int) | 704 myCh := make(chan int) |
| 705 c := cmd2{ true, 0, myCh } // Composite literal syntax. | 705 c := cmd2{ true, 0, myCh } // Composite literal syntax. |
| 706 ch <- c | 706 ch <- c |
| 707 return <-myCh | 707 return <-myCh |
| 708 } | 708 } |
| 709 </pre> | 709 </pre> |
| 710 |
| 711 <h2 id="Appendix">Appendix</h2> |
| 712 <h3 id="First time pitfall">First Taste Pitfall</h3> |
| 713 |
| 714 <h4><a href="go_spec.html#Semicolons">Semicolons omit rule</a>. |
| 715 See <a href="#Syntax">Syntax</a>. |
| 716 </h4> |
| 717 <pre> |
| 718 func main() // compile error |
| 719 { |
| 720 ... |
| 721 } |
| 722 func main() { // OK |
| 723 ... |
| 724 } |
| 725 if x { |
| 726 } // compile error |
| 727 else { |
| 728 } |
| 729 </pre> |
| 730 |
| 731 <h4><a href="go_spec.html#Semicolons"> |
| 732 Separate long line into multi line by operator</a></h4> |
| 733 <pre> |
| 734 s := "line1 \ // compile error |
| 735 line2" |
| 736 s := "line1" // compile error |
| 737 + "line2" |
| 738 s := "line1" + // use operator, OK |
| 739 "line2" |
| 740 s := `line1 // use backquote, OK |
| 741 line2` |
| 742 // string contain backquote(`) should use '+' or function to replace |
| 743 s := "...`..." // method 1, compile time decide |
| 744 s := strings.Replace(`...X....`,"X","'",-1) // method 2, runtime replace |
| 745 </pre> |
| 746 <h4>type cast</h4> |
| 747 <ul> |
| 748 <li>convert float to []byte by <a href="../pkg/encoding/binary/#Write">binary</a ></li> |
| 749 </ul> |
| 750 <h4>array declaration, reference <a href="#Syntax">Syntax</a></h4> |
| 751 For multiple dimension use following trick for speedup,· |
| 752 ref <a href="http://blog.golang.org/2011/06/profiling-go-programs.html"> |
| 753 profiling</a> |
| 754 <pre> |
| 755 const rows,cols=4,7 |
| 756 |
| 757 m := [rows][cols]byte{} |
| 758 // TODO: reduce rows*cols to 1 + rows allocation when byte is large type. |
| 759 |
| 760 for y,i:=0,0; y<rows; y++ { |
| 761 for x:=0; x<cols; x,i = x+1, i+1 { |
| 762 m[y][x]= '0' + byte(i) |
| 763 } |
| 764 } |
| 765 // display it |
| 766 for y:=0; y<rows; y++ { |
| 767 for x:=0; x<cols; x++ { |
| 768 fmt.Printf("%c",m[y][x]) |
| 769 } |
| 770 fmt.Print("\n") |
| 771 } |
| 772 </pre> |
| 773 |
| 774 |
| 775 <h3 id="reserved_keyword_mapping">Reserved Keyword Mapping</h3> |
| 776 <table class="codetable" frame="border" summary="Reserved Keyword Mapping"> |
| 777 <colgroup align="left" width="10%"></colgroup> |
| 778 <colgroup align="left" width="90%"></colgroup> |
| 779 <tr> |
| 780 <th align="left">C/C++</th> |
| 781 <th align="left">Go</th> |
| 782 </tr> |
| 783 <tr> |
| 784 <td colspan="2"><hr></td> |
| 785 </tr> |
| 786 <!--C reserved words --> |
| 787 <tr><td>auto</td> <td>X</td></tr> |
| 788 <tr><td>break</td> <td>break [Label] to outer loop</td></tr> |
| 789 <tr><td>case</td> <td>case can accept multiple values</td></tr> |
| 790 <tr><td>char</td> <td>int8/uint8/byte</td></tr> |
| 791 <tr><td>const</td> <td>only for define constant variable in global/function</td> </tr> |
| 792 <tr><td>continue</td> <td>continue [Label] to outer loop</td></tr> |
| 793 <tr><td>default</td> <td>default</td></tr> |
| 794 <tr><td valign="top">do</td> <td><a href="go_spec.html#For_statements">for</a> |
| 795 <pre> |
| 796 for bFirst:=true ; bFirst || expr ; bFirst = false { |
| 797 ... |
| 798 } |
| 799 </pre></td></tr> |
| 800 <tr><td>double</td> <td>float64</td></tr> |
| 801 <tr><td>else</td> <td>else</td></tr> |
| 802 <tr><td valign="top">enum</td> <td> |
| 803 <pre> |
| 804 // use const and/or itoa instead |
| 805 type mymask uint8 |
| 806 const ( |
| 807 m1 = mymask(1<<iota) |
| 808 m2 |
| 809 m4 |
| 810 m5 = mymask(5) |
| 811 ) |
| 812 </pre> |
| 813 <tr><td>extern</td> <td>all global declaration which name lead with uppercase |
| 814 </td></tr> |
| 815 <tr><td>float</td> <td>float32</td></tr> |
| 816 <tr><td>for</td> <td><a href="go_spec.html#For_statements">for</a></td></tr> |
| 817 <tr><td>goto</td> <td>goto</td></tr> |
| 818 <tr><td>if</td> <td>if</td></tr> |
| 819 <tr><td>int</td> <td><a href="../pkg/builtin#int">int</a> is not alias of int32, use int8,uint16,... |
| 820 </td></tr> |
| 821 <tr><td>long</td> <td>int32 for 4 bytes, int64 for 8 bytes</td></tr> |
| 822 <tr><td>register</td> <td>X</td></tr> |
| 823 <tr><td>return</td> <td>could return multiple values</td></tr> |
| 824 <tr><td>short</td> <td>int16</td></tr> |
| 825 <tr><td>signed</td> <td>uint,uint8,uint16,uint32,uint64</td></tr> |
| 826 <tr><td>sizeof</td> <td>unsafe.Sizeof</td></tr> |
| 827 <tr><td valign="top">static</td> <td><pre> |
| 828 // use closure method to implement static variable |
| 829 func f() func() int { |
| 830 i := 100 |
| 831 return func() int { i++ ; return i } |
| 832 } |
| 833 func main() { |
| 834 next := f() |
| 835 println(next(), next(), next()) |
| 836 } |
| 837 </pre></td></tr> |
| 838 <tr><td>struct</td> <td><a href="go_spec.html#Struct_types">struct</a></td></tr> |
| 839 <tr><td valign="top">switch</td> <td><a href="go_spec.html#Switch_statements">sw itch</a> |
| 840 accept not only integers. |
| 841 <pre> |
| 842 switch i { // i=1/2/3 will only produce one line output |
| 843 case 1: case 2: println("It is", i) |
| 844 case 3: println("It's three") |
| 845 } |
| 846 // is equal to |
| 847 switch i { |
| 848 case 1, 2: println("It is", i) |
| 849 case 3: println("It's three") |
| 850 } |
| 851 // the code with same logic as C/C++ code, show two line output for i==1 |
| 852 switch i { |
| 853 case 1: case 2: println("It is", i); fallthrough |
| 854 case 3: println("It's three") |
| 855 } |
| 856 </pre> |
| 857 </td></tr> |
| 858 <tr><td>typedef</td> <td><a href="go_spec.html#Types">type</a></td></tr> |
| 859 <tr><td>union</td> <td>X</td></tr> |
| 860 <tr><td>unsigned</td> <td>uint8,uint16,uint32,uint64</td></tr> |
| 861 <tr><td>void</td> <td>X</td></tr> |
| 862 <tr><td>volatile</td> <td>X</td></tr> |
| 863 <tr><td>while</td> <td><a href="go_spec.html#For_statements">for</a></td></tr> |
| 864 |
| 865 <!--C++ reserved words --> |
| 866 <td colspan="2"><hr></td> |
| 867 <tr><td>asm</td> <td>try to import C by <a href="../cmd/cgo">cgo</a></td></tr> |
| 868 <tr><td>dynamic_cast</td> <td>X</td></tr> |
| 869 <tr><td>namespace</td> <td>X</td></tr> |
| 870 <tr><td>reinterpret_cast</td> <td>X</td></tr> |
| 871 <tr><td>try</td> <td><a href="go_spec.html#Handling_panics">panic</a> |
| 872 </td></tr> |
| 873 <tr><td>bool</td> <td>bool</td></tr> |
| 874 <tr><td>explicit</td> <td>X</td></tr> |
| 875 <tr><td>new</td> <td><a href="go_spec.html#Allocation">new/make</a></td></tr> |
| 876 <tr><td>static_cast</td> <td>X</td></tr> |
| 877 <tr><td>typeid</td> <td>X</td></tr> |
| 878 <tr><td>catch</td> <td><a href="go_spec.html#Handling_panics">panic</a> |
| 879 <tr><td>false</td> <td>false</td></tr> |
| 880 <tr><td>operator</td> <td>X</td></tr> |
| 881 <tr><td>template</td> <td>X</td></tr> |
| 882 <tr><td>typename</td> <td>X</td></tr> |
| 883 <tr><td>class</td> <td><a href="#Interfaces">type + interface</a></td></tr> |
| 884 <tr><td>friend</td> <td>X</td></tr> |
| 885 <tr><td>private</td> <td>name's first char prefix with lowercase</td></tr> |
| 886 <tr><td>this</td> <td>X</td></tr> |
| 887 <tr><td>using</td> <td>X</td></tr> |
| 888 <tr><td>const_cast</td> <td>X</td></tr> |
| 889 <tr><td>inline</td> <td>X</td></tr> |
| 890 <tr><td>public</td> <td>name's first char prefix with uppercase</td></tr> |
| 891 <tr><td>throw</td> <td>X</td></tr> |
| 892 <tr><td>virtual</td> <td>X</td></tr> |
| 893 <tr><td>delete</td> |
| 894 <td>automatic garbage collection, delete(m,k) for del map item</td></tr> |
| 895 <tr><td>mutable</td> <td>X</td></tr> |
| 896 <tr><td>protected</td> <td>X</td></tr> |
| 897 <tr><td>true</td> <td>true</td></tr> |
| 898 <tr><td>wchar_t</td> <td>rune</td></tr> |
| 899 |
| 900 <!--some predefined identifiers --> |
| 901 <td colspan="2"><hr></td> |
| 902 <tr><td>cin</td> <td>os.Stdin</td></tr> |
| 903 <tr><td>cout</td> <td>os.Stdout</td></tr> |
| 904 <tr><td>endl</td> <td>use Println(), xxxln(), or "\n"</td></tr> |
| 905 <tr><td>include</td> <td><a href="go_spec.html#Import_declarations">import</a>, |
| 906 see also "go help importpath"</td></tr> |
| 907 <tr><td>INT_MIN</td> <td>X</td></tr> |
| 908 <tr><td>INT_MAX</td> <td>X</td></tr> |
| 909 <tr><td>iomanip</td> <td><a href="../pkg/fmt">fmt</a></td></tr> |
| 910 <tr><td>iostream</td> <td> |
| 911 <a href="../pkg/bufio">bufio</a>, |
| 912 <a href="../pkg/fmt">fmt</a>, |
| 913 <a href="../pkg/io">io</a>, |
| 914 <a href="../pkg/io/ioutil">io/ioutil</a> |
| 915 </td></tr> |
| 916 <tr><td>main</td> <td><a href="go_spec.html#Packages">package</a>, |
| 917 To return error code to OS, use os.Exit() instead of return from main() |
| 918 </td></tr> |
| 919 <tr><td>MAX_RAND</td> <td>X</td></tr> |
| 920 <tr><td>NULL</td> <td>nil</td></tr> |
| 921 <tr><td>string</td> <td> |
| 922 <a href="go_spec.html#String_types">string</a>,· |
| 923 see also <a href="../pkg/strconv">strconv</a> |
| 924 </td></tr> |
| 925 <tr> |
| 926 <td colspan="2"><hr></td> |
| 927 </tr> |
| 928 </table> |
| 929 |
| 930 |
| 931 <h3 id="other_mapping">Other Mapping</h3> |
| 932 |
| 933 <table class="codetable" frame="border" summary="Other Mapping"> |
| 934 <colgroup align="left" width="10%"></colgroup> |
| 935 <colgroup align="left" width="90%"></colgroup> |
| 936 <tr> |
| 937 <th align="left">C/C++</th> |
| 938 <th align="left">Go</th> |
| 939 </tr> |
| 940 <!-- common programing behavior --> |
| 941 <tr><td colspan="2"><hr></td></tr> |
| 942 <tr><td>argc</td> <td>len(os.Args)</td></tr> |
| 943 <tr><td>argv</td> <td>os.Args</td></tr> |
| 944 |
| 945 <!-- common functions --> |
| 946 <tr><td colspan="2"><hr></td></tr> |
| 947 <tr><td>atof</td> <td>strconv.parseFloat</td></tr> |
| 948 <tr><td>atoi</td> <td>strconv.ParseFloat</td></tr> |
| 949 <tr><td>getopt</td> <td><a href="../pkg/flag">flag</a></td></tr> |
| 950 <tr><td>itoa</td> <td>strconv.Itoa/FormatInt/FormatUint</td></tr> |
| 951 <tr><td valign="top">printf</td> <td>fmt.Print/Printf/Println. |
| 952 Go's Print is powerful, it could print any type with String() member. |
| 953 For xxxf() function, we can use "go tool vet *.go" to validate % flags |
| 954 </td></tr> |
| 955 <tr><td>scanf</td> <td>fmt.Scan/Scanf/Scanln</td></tr> |
| 956 <tr><td>sscanf</td> <td>fmt.Sscan/Sscanf/Sscanln</td></tr> |
| 957 <tr><td>sprintf</td> <td>fmt.Sprint/Sprintf/Sprintln</td></tr> |
| 958 |
| 959 <!-- C++ STL --> |
| 960 <tr><td colspan="2"><hr></td></tr> |
| 961 <tr><td>bitset</td> <td>X</td></tr> |
| 962 <tr><td>deque</td> <td><a href="../pkg/container/list">container/list</a></td></ tr> |
| 963 <tr><td>hashmap</td> <td><a href="go_spec.html#Map_types">map</a></td></tr> |
| 964 <tr><td>list</td> <td><a href="../pkg/container/list">container/list</a></td></t r> |
| 965 <tr><td valign="top">map</td> <td><a href="go_spec.html#Map_types">map</a> |
| 966 like C++'s hashmap, it is unordered. |
| 967 <pre> |
| 968 var m = map[string]int{ "Mon":1, "Tue":2 } |
| 969 for k,v := range m { fmt.Println("k=",k,"v=",v"} // display |
| 970 // NOTE: iterate order is dynamic, don't treat it is consistent |
| 971 m["Wed"] = 2 // insert |
| 972 m["Wed"] = 3 // update |
| 973 delete(m,"Wed") // delete |
| 974 var m2 = make(map[string]string, 1e4) // pre-allocate initial size |
| 975 </pre> |
| 976 </tr> |
| 977 <tr><td>multiset</td> <td>X</td></tr> |
| 978 <tr><td>multimap</td> <td>X</td></tr> |
| 979 <tr><td valign="top">queue</td> <td> |
| 980 In general case, use <a href="go_spec.html#Slices">slice</a> instead. |
| 981 Or, use <a href="../pkg/container/list">container/list</a> |
| 982 if don't like the slice memory allocation. |
| 983 Code come from <a href="http://code.google.com/p/go-wiki/wiki/SliceTricks"> |
| 984 SliceTricks</a><pre> |
| 985 var q = []string |
| 986 q = append(q, "Jan") // push |
| 987 q = append(q, "Feb") // push |
| 988 v, q = q[len(q)-1], q[:len(q)-1] // pop, use trick of assign multiple value |
| 989 </pre></td></tr> |
| 990 <tr><td>priority_queue</td> <td>X</td></tr> |
| 991 <tr><td>set</td> <td>X</td></tr> |
| 992 <tr><td>stack</td> <td><a href="../pkg/container/list">container/list</a></td></ tr> |
| 993 <tr><td valign="top">vector</td> <td> |
| 994 use <a href="go_spec.html#Slices">slice</a> instead. |
| 995 code come from <a href="http://code.google.com/p/go-wiki/wiki/SliceTricks"> |
| 996 SliceTricks</a><pre> |
| 997 var v = []int |
| 998 v = append(v, 3) // append an element |
| 999 var v2 = []int{5,6} |
| 1000 for i,v := range(v) { fmt.Println("index=", i, "value=", v)} // display |
| 1001 v = append(v, v2...) // append another vector |
| 1002 v = append(a[:1], append([]int{4}, a[1:]...)...) // insert 4 before v[1] |
| 1003 v[3]=99 // update |
| 1004 v = append(a[:1], a[2:]...) // delete element v[1] |
| 1005 </pre></td></tr> |
| 1006 |
| 1007 <tr> <td colspan="2"><hr></td> </tr> |
| 1008 </table> |
| OLD | NEW |