Go 語言 與 C 語言 的比較學習

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

對於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

C Go
a->b a.b

函數閉包

C Go

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}
  • Interface
    定義介面,說明函數類型
type IOOTest interface {    GetAByPointer() int }

使用的時候將實現了對應的介面的結構體對象指標賦值給介面對象

var i_test IOOTestvar test OOTest i_test = &test
  • Combination
    沒啥子好說的

  • Inherit ( by anonymous combination )

type OOTest1 struct{    OOTest    b int}

這樣 OOTest1 就擁有了 OOTest 的成員變數和方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.