這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
對於MarkDown 編譯器沒有自動儲存功能這件事情, 我表示嚴重的厭惡。 一個來小時的整理化為烏有,而且居然還不能匯入到HTML編輯器, 真是無法忍受!
關鍵字
分類 |
C |
Go |
字元 |
char |
無 |
字串 |
無 |
string |
浮點數 |
double , float |
float32 , float64 , complex64 , complex128 |
整數 |
int long short |
uint8 , uint16 , uint32 , uint64 , int8 , int16 , int32 , int64 , uint , int , uintptr , byte = uint8 , rune=unit32 |
bool |
無 |
boolean true , false |
函數 |
無 |
func |
介面 |
無 |
interface |
空 |
void |
無 |
Any |
無 |
interface{} |
彙總 |
enum , struct , union |
struct |
符號限定符號 |
signed , unsigned , auto , const , extern , register , static , volatile |
auto , const |
流程式控制制 |
break , case , do , for , goto , if , else , continue , default , return , switch , while |
for , if , else , goto , break , switch , case , default , fallthrough , range , select , range |
資料結構 |
無 |
map |
其他 |
sizeof , typedef |
iota , var , type , defer , go chan , package , import |
操作符
功能 |
C |
Go |
取反 |
~ |
^ |
選擇 |
? : |
|
取指標內容 |
-> |
|
寫入讀出 |
|
<- -> |
初始化並賦值 |
|
:= |
其餘的運算子基本相同
變數
X |
C |
Go |
聲明/定義 |
TYPE NAME int i |
(var) NAME TYPE var i int |
字元(串) |
char , char [] |
byte byte[], string |
整形位寬 |
依賴於特定的平台和編譯器 |
可以指定寬度, 也可以依賴平台編譯器 |
有效範圍 |
棧上開闢的變數在範圍內有效, 堆上開闢的變數手動釋放之前有效 |
最後一個引用離開範圍之前都有效 |
範圍控制 |
static 限制於本檔案有效 , extern 尋找其他檔案的全域變數 |
大寫開頭可以匯出, 小寫僅限本包使用 |
指標
假設a
是一個指向某結構體的指標,這個結構體中有個成員叫b
。
函數閉包
PO
X |
C |
Go |
if |
“if ( condition) XXX else XXX“` |
“`if condition {XXX} else {XXX} |
switch |
只能選擇可以轉化為整形的 , case中沒有break自動繼續 |
可以任意類型, 不會自動繼續出發使用failthrough |
select |
是個函數 |
關鍵字, 選擇chan |
for |
for ( ; ; ) XXX |
for ; ; {XXX} |
while |
有 |
無 |
do … while |
有 |
無 |
編程規範
- 句末不需要分號
- if for 的程式碼片段必須被大括弧包圍
- if 的 condition 不要括弧
- for 的 指示部分不要括弧
- 左花括弧不能自成一行
OO
- Method
在關鍵字 func 和函數名字之間說明屬於那個結構體。
type OOTest struct { a int}func ( this * OOTest) GetAByPointer() int { return this.a}func ( this OOTest) GetAByCopy() int { return this.a}
type IOOTest interface { GetAByPointer() int }
使用的時候將實現了對應的介面的結構體對象指標賦值給介面對象
var i_test IOOTestvar test OOTest i_test = &test
type OOTest1 struct{ OOTest b int}
這樣 OOTest1
就擁有了 OOTest
的成員變數和方法