2015-12-09 07:57:17 +00:00
<!DOCTYPE HTML>
< html lang = "zh-tw" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" / >
< title > 通過cgo調用C代碼 | Go编程语言< / title >
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "" >
< meta name = "generator" content = "GitBook 2.5.2" >
< meta name = "HandheldFriendly" content = "true" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1, user-scalable=no" >
< meta name = "apple-mobile-web-app-capable" content = "yes" >
< meta name = "apple-mobile-web-app-status-bar-style" content = "black" >
< link rel = "apple-touch-icon-precomposed" sizes = "152x152" href = "../gitbook/images/apple-touch-icon-precomposed-152.png" >
< link rel = "shortcut icon" href = "../gitbook/images/favicon.ico" type = "image/x-icon" >
< link rel = "stylesheet" href = "../gitbook/style.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-highlight/website.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-search/search.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-fontsettings/website.css" >
< link rel = "next" href = "../ch13/ch13-05.html" / >
< link rel = "prev" href = "../ch13/ch13-03.html" / >
< / head >
< body >
2015-12-16 02:56:29 +00:00
< div class = "book" data-level = "13.4" data-chapter-title = "通過cgo調用C代碼" data-filepath = "ch13/ch13-04.md" data-basepath = ".." data-revision = "Wed Dec 16 2015 10:54:29 GMT+0800 (中国标准时间)" >
2015-12-09 07:57:17 +00:00
< div class = "book-summary" >
< nav role = "navigation" >
< ul class = "summary" >
< li class = "chapter " data-level = "0" data-path = "index.html" >
< a href = "../index.html" >
< i class = "fa fa-check" > < / i >
前言
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "0.1" data-path = "ch0/ch0-01.html" >
< a href = "../ch0/ch0-01.html" >
< i class = "fa fa-check" > < / i >
< b > 0.1.< / b >
Go語言起源
< / a >
< / li >
< li class = "chapter " data-level = "0.2" data-path = "ch0/ch0-02.html" >
< a href = "../ch0/ch0-02.html" >
< i class = "fa fa-check" > < / i >
< b > 0.2.< / b >
Go語言項目
< / a >
< / li >
< li class = "chapter " data-level = "0.3" data-path = "ch0/ch0-03.html" >
< a href = "../ch0/ch0-03.html" >
< i class = "fa fa-check" > < / i >
< b > 0.3.< / b >
本書的組織
< / a >
< / li >
< li class = "chapter " data-level = "0.4" data-path = "ch0/ch0-04.html" >
< a href = "../ch0/ch0-04.html" >
< i class = "fa fa-check" > < / i >
< b > 0.4.< / b >
更多的信息
< / a >
< / li >
< li class = "chapter " data-level = "0.5" data-path = "ch0/ch0-05.html" >
< a href = "../ch0/ch0-05.html" >
< i class = "fa fa-check" > < / i >
< b > 0.5.< / b >
緻謝
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "1" data-path = "ch1/ch1.html" >
< a href = "../ch1/ch1.html" >
< i class = "fa fa-check" > < / i >
< b > 1.< / b >
入門
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "1.1" data-path = "ch1/ch1-01.html" >
< a href = "../ch1/ch1-01.html" >
< i class = "fa fa-check" > < / i >
< b > 1.1.< / b >
Hello, World
< / a >
< / li >
< li class = "chapter " data-level = "1.2" data-path = "ch1/ch1-02.html" >
< a href = "../ch1/ch1-02.html" >
< i class = "fa fa-check" > < / i >
< b > 1.2.< / b >
命令行參數
< / a >
< / li >
< li class = "chapter " data-level = "1.3" data-path = "ch1/ch1-03.html" >
< a href = "../ch1/ch1-03.html" >
< i class = "fa fa-check" > < / i >
< b > 1.3.< / b >
査找重復的行
< / a >
< / li >
< li class = "chapter " data-level = "1.4" data-path = "ch1/ch1-04.html" >
< a href = "../ch1/ch1-04.html" >
< i class = "fa fa-check" > < / i >
< b > 1.4.< / b >
GIF動畫
< / a >
< / li >
< li class = "chapter " data-level = "1.5" data-path = "ch1/ch1-05.html" >
< a href = "../ch1/ch1-05.html" >
< i class = "fa fa-check" > < / i >
< b > 1.5.< / b >
穫取URL
< / a >
< / li >
< li class = "chapter " data-level = "1.6" data-path = "ch1/ch1-06.html" >
< a href = "../ch1/ch1-06.html" >
< i class = "fa fa-check" > < / i >
< b > 1.6.< / b >
併髮穫取多個URL
< / a >
< / li >
< li class = "chapter " data-level = "1.7" data-path = "ch1/ch1-07.html" >
< a href = "../ch1/ch1-07.html" >
< i class = "fa fa-check" > < / i >
< b > 1.7.< / b >
Web服務
< / a >
< / li >
< li class = "chapter " data-level = "1.8" data-path = "ch1/ch1-08.html" >
< a href = "../ch1/ch1-08.html" >
< i class = "fa fa-check" > < / i >
< b > 1.8.< / b >
本章要點
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "2" data-path = "ch2/ch2.html" >
< a href = "../ch2/ch2.html" >
< i class = "fa fa-check" > < / i >
< b > 2.< / b >
程序結構
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "2.1" data-path = "ch2/ch2-01.html" >
< a href = "../ch2/ch2-01.html" >
< i class = "fa fa-check" > < / i >
< b > 2.1.< / b >
命名
< / a >
< / li >
< li class = "chapter " data-level = "2.2" data-path = "ch2/ch2-02.html" >
< a href = "../ch2/ch2-02.html" >
< i class = "fa fa-check" > < / i >
< b > 2.2.< / b >
聲明
< / a >
< / li >
< li class = "chapter " data-level = "2.3" data-path = "ch2/ch2-03.html" >
< a href = "../ch2/ch2-03.html" >
< i class = "fa fa-check" > < / i >
< b > 2.3.< / b >
變量
< / a >
< / li >
< li class = "chapter " data-level = "2.4" data-path = "ch2/ch2-04.html" >
< a href = "../ch2/ch2-04.html" >
< i class = "fa fa-check" > < / i >
< b > 2.4.< / b >
賦值
< / a >
< / li >
< li class = "chapter " data-level = "2.5" data-path = "ch2/ch2-05.html" >
< a href = "../ch2/ch2-05.html" >
< i class = "fa fa-check" > < / i >
< b > 2.5.< / b >
類型
< / a >
< / li >
< li class = "chapter " data-level = "2.6" data-path = "ch2/ch2-06.html" >
< a href = "../ch2/ch2-06.html" >
< i class = "fa fa-check" > < / i >
< b > 2.6.< / b >
包和文件
< / a >
< / li >
< li class = "chapter " data-level = "2.7" data-path = "ch2/ch2-07.html" >
< a href = "../ch2/ch2-07.html" >
< i class = "fa fa-check" > < / i >
< b > 2.7.< / b >
作用域
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "3" data-path = "ch3/ch3.html" >
< a href = "../ch3/ch3.html" >
< i class = "fa fa-check" > < / i >
< b > 3.< / b >
基礎數據類型
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "3.1" data-path = "ch3/ch3-01.html" >
< a href = "../ch3/ch3-01.html" >
< i class = "fa fa-check" > < / i >
< b > 3.1.< / b >
整型
< / a >
< / li >
< li class = "chapter " data-level = "3.2" data-path = "ch3/ch3-02.html" >
< a href = "../ch3/ch3-02.html" >
< i class = "fa fa-check" > < / i >
< b > 3.2.< / b >
浮點數
< / a >
< / li >
< li class = "chapter " data-level = "3.3" data-path = "ch3/ch3-03.html" >
< a href = "../ch3/ch3-03.html" >
< i class = "fa fa-check" > < / i >
< b > 3.3.< / b >
復數
< / a >
< / li >
< li class = "chapter " data-level = "3.4" data-path = "ch3/ch3-04.html" >
< a href = "../ch3/ch3-04.html" >
< i class = "fa fa-check" > < / i >
< b > 3.4.< / b >
佈爾型
< / a >
< / li >
< li class = "chapter " data-level = "3.5" data-path = "ch3/ch3-05.html" >
< a href = "../ch3/ch3-05.html" >
< i class = "fa fa-check" > < / i >
< b > 3.5.< / b >
字符串
< / a >
< / li >
< li class = "chapter " data-level = "3.6" data-path = "ch3/ch3-06.html" >
< a href = "../ch3/ch3-06.html" >
< i class = "fa fa-check" > < / i >
< b > 3.6.< / b >
常量
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "4" data-path = "ch4/ch4.html" >
< a href = "../ch4/ch4.html" >
< i class = "fa fa-check" > < / i >
< b > 4.< / b >
復閤數據類型
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.1" data-path = "ch4/ch4-01.html" >
< a href = "../ch4/ch4-01.html" >
< i class = "fa fa-check" > < / i >
< b > 4.1.< / b >
數組
< / a >
< / li >
< li class = "chapter " data-level = "4.2" data-path = "ch4/ch4-02.html" >
< a href = "../ch4/ch4-02.html" >
< i class = "fa fa-check" > < / i >
< b > 4.2.< / b >
切片
< / a >
< / li >
< li class = "chapter " data-level = "4.3" data-path = "ch4/ch4-03.html" >
< a href = "../ch4/ch4-03.html" >
< i class = "fa fa-check" > < / i >
< b > 4.3.< / b >
字典
< / a >
< / li >
< li class = "chapter " data-level = "4.4" data-path = "ch4/ch4-04.html" >
< a href = "../ch4/ch4-04.html" >
< i class = "fa fa-check" > < / i >
< b > 4.4.< / b >
結構體
< / a >
< / li >
< li class = "chapter " data-level = "4.5" data-path = "ch4/ch4-05.html" >
< a href = "../ch4/ch4-05.html" >
< i class = "fa fa-check" > < / i >
< b > 4.5.< / b >
JSON
< / a >
< / li >
< li class = "chapter " data-level = "4.6" data-path = "ch4/ch4-06.html" >
< a href = "../ch4/ch4-06.html" >
< i class = "fa fa-check" > < / i >
< b > 4.6.< / b >
文本和HTML模闆
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5" data-path = "ch5/ch5.html" >
< a href = "../ch5/ch5.html" >
< i class = "fa fa-check" > < / i >
< b > 5.< / b >
函數
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.1" data-path = "ch5/ch5-01.html" >
< a href = "../ch5/ch5-01.html" >
< i class = "fa fa-check" > < / i >
< b > 5.1.< / b >
函數聲明
< / a >
< / li >
< li class = "chapter " data-level = "5.2" data-path = "ch5/ch5-02.html" >
< a href = "../ch5/ch5-02.html" >
< i class = "fa fa-check" > < / i >
< b > 5.2.< / b >
遞歸
< / a >
< / li >
< li class = "chapter " data-level = "5.3" data-path = "ch5/ch5-03.html" >
< a href = "../ch5/ch5-03.html" >
< i class = "fa fa-check" > < / i >
< b > 5.3.< / b >
多返迴值
< / a >
< / li >
< li class = "chapter " data-level = "5.4" data-path = "ch5/ch5-04.html" >
< a href = "../ch5/ch5-04.html" >
< i class = "fa fa-check" > < / i >
< b > 5.4.< / b >
錯誤
< / a >
< / li >
< li class = "chapter " data-level = "5.5" data-path = "ch5/ch5-05.html" >
< a href = "../ch5/ch5-05.html" >
< i class = "fa fa-check" > < / i >
< b > 5.5.< / b >
函數值
< / a >
< / li >
< li class = "chapter " data-level = "5.6" data-path = "ch5/ch5-06.html" >
< a href = "../ch5/ch5-06.html" >
< i class = "fa fa-check" > < / i >
< b > 5.6.< / b >
匿名函數
< / a >
< / li >
< li class = "chapter " data-level = "5.7" data-path = "ch5/ch5-07.html" >
< a href = "../ch5/ch5-07.html" >
< i class = "fa fa-check" > < / i >
< b > 5.7.< / b >
可變參數
< / a >
< / li >
< li class = "chapter " data-level = "5.8" data-path = "ch5/ch5-08.html" >
< a href = "../ch5/ch5-08.html" >
< i class = "fa fa-check" > < / i >
< b > 5.8.< / b >
Deferred函數
< / a >
< / li >
< li class = "chapter " data-level = "5.9" data-path = "ch5/ch5-09.html" >
< a href = "../ch5/ch5-09.html" >
< i class = "fa fa-check" > < / i >
< b > 5.9.< / b >
Panic異常
< / a >
< / li >
< li class = "chapter " data-level = "5.10" data-path = "ch5/ch5-10.html" >
< a href = "../ch5/ch5-10.html" >
< i class = "fa fa-check" > < / i >
< b > 5.10.< / b >
Recover捕穫異常
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6" data-path = "ch6/ch6.html" >
< a href = "../ch6/ch6.html" >
< i class = "fa fa-check" > < / i >
< b > 6.< / b >
方法
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.1" data-path = "ch6/ch6-01.html" >
< a href = "../ch6/ch6-01.html" >
< i class = "fa fa-check" > < / i >
< b > 6.1.< / b >
方法聲明
< / a >
< / li >
< li class = "chapter " data-level = "6.2" data-path = "ch6/ch6-02.html" >
< a href = "../ch6/ch6-02.html" >
< i class = "fa fa-check" > < / i >
< b > 6.2.< / b >
基於指鍼對象的方法
< / a >
< / li >
< li class = "chapter " data-level = "6.3" data-path = "ch6/ch6-03.html" >
< a href = "../ch6/ch6-03.html" >
< i class = "fa fa-check" > < / i >
< b > 6.3.< / b >
通過嵌入結構體來擴展類型
< / a >
< / li >
< li class = "chapter " data-level = "6.4" data-path = "ch6/ch6-04.html" >
< a href = "../ch6/ch6-04.html" >
< i class = "fa fa-check" > < / i >
< b > 6.4.< / b >
方法值和方法錶達式
< / a >
< / li >
< li class = "chapter " data-level = "6.5" data-path = "ch6/ch6-05.html" >
< a href = "../ch6/ch6-05.html" >
< i class = "fa fa-check" > < / i >
< b > 6.5.< / b >
示例: Bit數組
< / a >
< / li >
< li class = "chapter " data-level = "6.6" data-path = "ch6/ch6-06.html" >
< a href = "../ch6/ch6-06.html" >
< i class = "fa fa-check" > < / i >
< b > 6.6.< / b >
封裝
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "7" data-path = "ch7/ch7.html" >
< a href = "../ch7/ch7.html" >
< i class = "fa fa-check" > < / i >
< b > 7.< / b >
接口
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "7.1" data-path = "ch7/ch7-01.html" >
< a href = "../ch7/ch7-01.html" >
< i class = "fa fa-check" > < / i >
< b > 7.1.< / b >
接口是閤約
< / a >
< / li >
< li class = "chapter " data-level = "7.2" data-path = "ch7/ch7-02.html" >
< a href = "../ch7/ch7-02.html" >
< i class = "fa fa-check" > < / i >
< b > 7.2.< / b >
接口類型
< / a >
< / li >
< li class = "chapter " data-level = "7.3" data-path = "ch7/ch7-03.html" >
< a href = "../ch7/ch7-03.html" >
< i class = "fa fa-check" > < / i >
< b > 7.3.< / b >
實現接口的條件
< / a >
< / li >
< li class = "chapter " data-level = "7.4" data-path = "ch7/ch7-04.html" >
< a href = "../ch7/ch7-04.html" >
< i class = "fa fa-check" > < / i >
< b > 7.4.< / b >
flag.Value接口
< / a >
< / li >
< li class = "chapter " data-level = "7.5" data-path = "ch7/ch7-05.html" >
< a href = "../ch7/ch7-05.html" >
< i class = "fa fa-check" > < / i >
< b > 7.5.< / b >
接口值
< / a >
< / li >
< li class = "chapter " data-level = "7.6" data-path = "ch7/ch7-06.html" >
< a href = "../ch7/ch7-06.html" >
< i class = "fa fa-check" > < / i >
< b > 7.6.< / b >
sort.Interface接口
< / a >
< / li >
< li class = "chapter " data-level = "7.7" data-path = "ch7/ch7-07.html" >
< a href = "../ch7/ch7-07.html" >
< i class = "fa fa-check" > < / i >
< b > 7.7.< / b >
http.Handler接口
< / a >
< / li >
< li class = "chapter " data-level = "7.8" data-path = "ch7/ch7-08.html" >
< a href = "../ch7/ch7-08.html" >
< i class = "fa fa-check" > < / i >
< b > 7.8.< / b >
error接口
< / a >
< / li >
< li class = "chapter " data-level = "7.9" data-path = "ch7/ch7-09.html" >
< a href = "../ch7/ch7-09.html" >
< i class = "fa fa-check" > < / i >
< b > 7.9.< / b >
示例: 錶達式求值
< / a >
< / li >
< li class = "chapter " data-level = "7.10" data-path = "ch7/ch7-10.html" >
< a href = "../ch7/ch7-10.html" >
< i class = "fa fa-check" > < / i >
< b > 7.10.< / b >
類型斷言
< / a >
< / li >
< li class = "chapter " data-level = "7.11" data-path = "ch7/ch7-11.html" >
< a href = "../ch7/ch7-11.html" >
< i class = "fa fa-check" > < / i >
< b > 7.11.< / b >
基於類型斷言識彆錯誤類型
< / a >
< / li >
< li class = "chapter " data-level = "7.12" data-path = "ch7/ch7-12.html" >
< a href = "../ch7/ch7-12.html" >
< i class = "fa fa-check" > < / i >
< b > 7.12.< / b >
通過類型斷言査詢接口
< / a >
< / li >
< li class = "chapter " data-level = "7.13" data-path = "ch7/ch7-13.html" >
< a href = "../ch7/ch7-13.html" >
< i class = "fa fa-check" > < / i >
< b > 7.13.< / b >
類型分支
< / a >
< / li >
< li class = "chapter " data-level = "7.14" data-path = "ch7/ch7-14.html" >
< a href = "../ch7/ch7-14.html" >
< i class = "fa fa-check" > < / i >
< b > 7.14.< / b >
示例: 基於標記的XML解碼
< / a >
< / li >
< li class = "chapter " data-level = "7.15" data-path = "ch7/ch7-15.html" >
< a href = "../ch7/ch7-15.html" >
< i class = "fa fa-check" > < / i >
< b > 7.15.< / b >
補充幾點
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "8" data-path = "ch8/ch8.html" >
< a href = "../ch8/ch8.html" >
< i class = "fa fa-check" > < / i >
< b > 8.< / b >
Goroutines和Channels
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.1" data-path = "ch8/ch8-01.html" >
< a href = "../ch8/ch8-01.html" >
< i class = "fa fa-check" > < / i >
< b > 8.1.< / b >
Goroutines
< / a >
< / li >
< li class = "chapter " data-level = "8.2" data-path = "ch8/ch8-02.html" >
< a href = "../ch8/ch8-02.html" >
< i class = "fa fa-check" > < / i >
< b > 8.2.< / b >
示例: 併髮的Clock服務
< / a >
< / li >
< li class = "chapter " data-level = "8.3" data-path = "ch8/ch8-03.html" >
< a href = "../ch8/ch8-03.html" >
< i class = "fa fa-check" > < / i >
< b > 8.3.< / b >
示例: 併髮的Echo服務
< / a >
< / li >
< li class = "chapter " data-level = "8.4" data-path = "ch8/ch8-04.html" >
< a href = "../ch8/ch8-04.html" >
< i class = "fa fa-check" > < / i >
< b > 8.4.< / b >
Channels
< / a >
< / li >
< li class = "chapter " data-level = "8.5" data-path = "ch8/ch8-05.html" >
< a href = "../ch8/ch8-05.html" >
< i class = "fa fa-check" > < / i >
< b > 8.5.< / b >
併行的循環
< / a >
< / li >
< li class = "chapter " data-level = "8.6" data-path = "ch8/ch8-06.html" >
< a href = "../ch8/ch8-06.html" >
< i class = "fa fa-check" > < / i >
< b > 8.6.< / b >
示例: 併髮的Web爬蟲
< / a >
< / li >
< li class = "chapter " data-level = "8.7" data-path = "ch8/ch8-07.html" >
< a href = "../ch8/ch8-07.html" >
< i class = "fa fa-check" > < / i >
< b > 8.7.< / b >
基於select的多路復用
< / a >
< / li >
< li class = "chapter " data-level = "8.8" data-path = "ch8/ch8-08.html" >
< a href = "../ch8/ch8-08.html" >
< i class = "fa fa-check" > < / i >
< b > 8.8.< / b >
示例: 併髮的字典遍歷
< / a >
< / li >
< li class = "chapter " data-level = "8.9" data-path = "ch8/ch8-09.html" >
< a href = "../ch8/ch8-09.html" >
< i class = "fa fa-check" > < / i >
< b > 8.9.< / b >
併髮的退齣
< / a >
< / li >
< li class = "chapter " data-level = "8.10" data-path = "ch8/ch8-10.html" >
< a href = "../ch8/ch8-10.html" >
< i class = "fa fa-check" > < / i >
< b > 8.10.< / b >
示例: 聊天服務
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "9" data-path = "ch9/ch9.html" >
< a href = "../ch9/ch9.html" >
< i class = "fa fa-check" > < / i >
< b > 9.< / b >
基於共享變量的併髮
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "9.1" data-path = "ch9/ch9-01.html" >
< a href = "../ch9/ch9-01.html" >
< i class = "fa fa-check" > < / i >
< b > 9.1.< / b >
競爭條件
< / a >
< / li >
< li class = "chapter " data-level = "9.2" data-path = "ch9/ch9-02.html" >
< a href = "../ch9/ch9-02.html" >
< i class = "fa fa-check" > < / i >
< b > 9.2.< / b >
sync.Mutex互斥鎖
< / a >
< / li >
< li class = "chapter " data-level = "9.3" data-path = "ch9/ch9-03.html" >
< a href = "../ch9/ch9-03.html" >
< i class = "fa fa-check" > < / i >
< b > 9.3.< / b >
sync.RWMutex讀寫鎖
< / a >
< / li >
< li class = "chapter " data-level = "9.4" data-path = "ch9/ch9-04.html" >
< a href = "../ch9/ch9-04.html" >
< i class = "fa fa-check" > < / i >
< b > 9.4.< / b >
內存衕步
< / a >
< / li >
< li class = "chapter " data-level = "9.5" data-path = "ch9/ch9-05.html" >
< a href = "../ch9/ch9-05.html" >
< i class = "fa fa-check" > < / i >
< b > 9.5.< / b >
sync.Once初始化
< / a >
< / li >
< li class = "chapter " data-level = "9.6" data-path = "ch9/ch9-06.html" >
< a href = "../ch9/ch9-06.html" >
< i class = "fa fa-check" > < / i >
< b > 9.6.< / b >
競爭條件檢測
< / a >
< / li >
< li class = "chapter " data-level = "9.7" data-path = "ch9/ch9-07.html" >
< a href = "../ch9/ch9-07.html" >
< i class = "fa fa-check" > < / i >
< b > 9.7.< / b >
示例: 併髮的非阻塞緩存
< / a >
< / li >
< li class = "chapter " data-level = "9.8" data-path = "ch9/ch9-08.html" >
< a href = "../ch9/ch9-08.html" >
< i class = "fa fa-check" > < / i >
< b > 9.8.< / b >
Goroutines和綫程
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "10" data-path = "ch10/ch10.html" >
< a href = "../ch10/ch10.html" >
< i class = "fa fa-check" > < / i >
< b > 10.< / b >
包和工具
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "10.1" data-path = "ch10/ch10-01.html" >
< a href = "../ch10/ch10-01.html" >
< i class = "fa fa-check" > < / i >
< b > 10.1.< / b >
簡介
< / a >
< / li >
< li class = "chapter " data-level = "10.2" data-path = "ch10/ch10-02.html" >
< a href = "../ch10/ch10-02.html" >
< i class = "fa fa-check" > < / i >
< b > 10.2.< / b >
導入路徑
< / a >
< / li >
< li class = "chapter " data-level = "10.3" data-path = "ch10/ch10-03.html" >
< a href = "../ch10/ch10-03.html" >
< i class = "fa fa-check" > < / i >
< b > 10.3.< / b >
包聲明
< / a >
< / li >
< li class = "chapter " data-level = "10.4" data-path = "ch10/ch10-04.html" >
< a href = "../ch10/ch10-04.html" >
< i class = "fa fa-check" > < / i >
< b > 10.4.< / b >
導入聲明
< / a >
< / li >
< li class = "chapter " data-level = "10.5" data-path = "ch10/ch10-05.html" >
< a href = "../ch10/ch10-05.html" >
< i class = "fa fa-check" > < / i >
< b > 10.5.< / b >
匿名導入
< / a >
< / li >
< li class = "chapter " data-level = "10.6" data-path = "ch10/ch10-06.html" >
< a href = "../ch10/ch10-06.html" >
< i class = "fa fa-check" > < / i >
< b > 10.6.< / b >
包和命名
< / a >
< / li >
< li class = "chapter " data-level = "10.7" data-path = "ch10/ch10-07.html" >
< a href = "../ch10/ch10-07.html" >
< i class = "fa fa-check" > < / i >
< b > 10.7.< / b >
工具
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "11" data-path = "ch11/ch11.html" >
< a href = "../ch11/ch11.html" >
< i class = "fa fa-check" > < / i >
< b > 11.< / b >
測試
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "11.1" data-path = "ch11/ch11-01.html" >
< a href = "../ch11/ch11-01.html" >
< i class = "fa fa-check" > < / i >
< b > 11.1.< / b >
go test
< / a >
< / li >
< li class = "chapter " data-level = "11.2" data-path = "ch11/ch11-02.html" >
< a href = "../ch11/ch11-02.html" >
< i class = "fa fa-check" > < / i >
< b > 11.2.< / b >
測試函數
< / a >
< / li >
< li class = "chapter " data-level = "11.3" data-path = "ch11/ch11-03.html" >
< a href = "../ch11/ch11-03.html" >
< i class = "fa fa-check" > < / i >
< b > 11.3.< / b >
測試覆蓋率
< / a >
< / li >
< li class = "chapter " data-level = "11.4" data-path = "ch11/ch11-04.html" >
< a href = "../ch11/ch11-04.html" >
< i class = "fa fa-check" > < / i >
< b > 11.4.< / b >
基準測試
< / a >
< / li >
< li class = "chapter " data-level = "11.5" data-path = "ch11/ch11-05.html" >
< a href = "../ch11/ch11-05.html" >
< i class = "fa fa-check" > < / i >
< b > 11.5.< / b >
剖析
< / a >
< / li >
< li class = "chapter " data-level = "11.6" data-path = "ch11/ch11-06.html" >
< a href = "../ch11/ch11-06.html" >
< i class = "fa fa-check" > < / i >
< b > 11.6.< / b >
示例函數
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "12" data-path = "ch12/ch12.html" >
< a href = "../ch12/ch12.html" >
< i class = "fa fa-check" > < / i >
< b > 12.< / b >
反射
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "12.1" data-path = "ch12/ch12-01.html" >
< a href = "../ch12/ch12-01.html" >
< i class = "fa fa-check" > < / i >
< b > 12.1.< / b >
為何需要反射?
< / a >
< / li >
< li class = "chapter " data-level = "12.2" data-path = "ch12/ch12-02.html" >
< a href = "../ch12/ch12-02.html" >
< i class = "fa fa-check" > < / i >
< b > 12.2.< / b >
reflect.Type和reflect.Value
< / a >
< / li >
< li class = "chapter " data-level = "12.3" data-path = "ch12/ch12-03.html" >
< a href = "../ch12/ch12-03.html" >
< i class = "fa fa-check" > < / i >
< b > 12.3.< / b >
Display遞歸打印
< / a >
< / li >
< li class = "chapter " data-level = "12.4" data-path = "ch12/ch12-04.html" >
< a href = "../ch12/ch12-04.html" >
< i class = "fa fa-check" > < / i >
< b > 12.4.< / b >
示例: 編碼S錶達式
< / a >
< / li >
< li class = "chapter " data-level = "12.5" data-path = "ch12/ch12-05.html" >
< a href = "../ch12/ch12-05.html" >
< i class = "fa fa-check" > < / i >
< b > 12.5.< / b >
通過reflect.Value脩改值
< / a >
< / li >
< li class = "chapter " data-level = "12.6" data-path = "ch12/ch12-06.html" >
< a href = "../ch12/ch12-06.html" >
< i class = "fa fa-check" > < / i >
< b > 12.6.< / b >
示例: 解碼S錶達式
< / a >
< / li >
< li class = "chapter " data-level = "12.7" data-path = "ch12/ch12-07.html" >
< a href = "../ch12/ch12-07.html" >
< i class = "fa fa-check" > < / i >
< b > 12.7.< / b >
穫取結構體字段標識
< / a >
< / li >
< li class = "chapter " data-level = "12.8" data-path = "ch12/ch12-08.html" >
< a href = "../ch12/ch12-08.html" >
< i class = "fa fa-check" > < / i >
< b > 12.8.< / b >
顯示一個類型的方法集
< / a >
< / li >
< li class = "chapter " data-level = "12.9" data-path = "ch12/ch12-09.html" >
< a href = "../ch12/ch12-09.html" >
< i class = "fa fa-check" > < / i >
< b > 12.9.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "13" data-path = "ch13/ch13.html" >
< a href = "../ch13/ch13.html" >
< i class = "fa fa-check" > < / i >
< b > 13.< / b >
底層編程
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "13.1" data-path = "ch13/ch13-01.html" >
< a href = "../ch13/ch13-01.html" >
< i class = "fa fa-check" > < / i >
< b > 13.1.< / b >
unsafe.Sizeof, Alignof 和 Offsetof
< / a >
< / li >
< li class = "chapter " data-level = "13.2" data-path = "ch13/ch13-02.html" >
< a href = "../ch13/ch13-02.html" >
< i class = "fa fa-check" > < / i >
< b > 13.2.< / b >
unsafe.Pointer
< / a >
< / li >
< li class = "chapter " data-level = "13.3" data-path = "ch13/ch13-03.html" >
< a href = "../ch13/ch13-03.html" >
< i class = "fa fa-check" > < / i >
< b > 13.3.< / b >
示例: 深度相等判斷
< / a >
< / li >
< li class = "chapter active" data-level = "13.4" data-path = "ch13/ch13-04.html" >
< a href = "../ch13/ch13-04.html" >
< i class = "fa fa-check" > < / i >
< b > 13.4.< / b >
通過cgo調用C代碼
< / a >
< / li >
< li class = "chapter " data-level = "13.5" data-path = "ch13/ch13-05.html" >
< a href = "../ch13/ch13-05.html" >
< i class = "fa fa-check" > < / i >
< b > 13.5.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "14" data-path = "exercise/ex.html" >
< a href = "../exercise/ex.html" >
< i class = "fa fa-check" > < / i >
< b > 14.< / b >
習題解答
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "14.1" data-path = "exercise/ex-ch1.html" >
< a href = "../exercise/ex-ch1.html" >
< i class = "fa fa-check" > < / i >
< b > 14.1.< / b >
第一章 入門
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "15" data-path = "errata.html" >
< a href = "../errata.html" >
< i class = "fa fa-check" > < / i >
< b > 15.< / b >
勘誤
< / a >
< / li >
< li class = "divider" > < / li >
< li >
< a href = "https://www.gitbook.com" target = "blank" class = "gitbook-link" >
本書使用 GitBook 釋出
< / a >
< / li >
< / ul >
< / nav >
< / div >
< div class = "book-body" >
< div class = "body-inner" >
< div class = "book-header" role = "navigation" >
<!-- Actions Left -->
<!-- Title -->
< h1 >
< i class = "fa fa-circle-o-notch fa-spin" > < / i >
< a href = "../" > Go编程语言< / a >
< / h1 >
< / div >
< div class = "page-wrapper" tabindex = "-1" role = "main" >
< div class = "page-inner" >
< section class = "normal" id = "section-" >
< h2 id = "134-通過cgo調用c代碼" > 13.4. 通 過 cgo調 用 C代 碼 < / h2 >
< p > Go程 序 可 能 會 遇 到 要 訪 問 C語 言 的 某 些 硬 件 驅 動 的 場 景 , 或 者 是 從 一 個 C++實 現 的 嵌 入 式 數 據 庫 査 詢 記 彔 的 場 景 , 或 者 是 使 用 Fortran實 現 的 一 些 綫 性 代 數 庫 的 場 景 . C作 為 一 個 通 用 語 言 , 很 多 庫 會 選 擇 提 供 一 個 C兼 容 的 API, 然 後 用 其 他 語 言 實 現 .< / p >
< p > 在 本 節 中 , 我 們 將 構 建 一 個 簡 易 的 數 據 壓 縮 程 序 , 通 過 使 用 一 個 Go語 言 自 帶 的 叫 cgo的 用 於 支 援 C語 言 函 數 調 用 的 工 具 . 這 類 工 具 被 稱 為 外 圍 函 數 接 口 (ffi), 併 且 cgo也 不 是 Go中 唯 一 的 類 似 工 具 . SWIG(swig.org) 是 類 似 的 另 一 個 被 廣 氾 使 用 的 工 具 , 它 提 供 了 很 多 復 雜 特 性 以 支 援 C++的 集 成 , 但 SWIG 不 是 這 裏 要 討 論 的 主 題 .< / p >
< p > 在 標 準 庫 的 < code > compress/...< / code > 子 目 彔 有 很 多 流 行 的 壓 縮 算 法 的 編 碼 和 解 碼 實 現 , 包 括 LZW壓 縮 算 法 (Unix的 compress命 令 用 的 算 法 )和 DEFLATE壓 縮 算 法 (GNU gzip命 令 用 的 算 法 ). 這 些 包 的 API的 細 節 有 些 差 異 , 但 是 它 們 都 提 供 了 鍼 對 < code > io.Writer< / code > 的 壓 縮 接 口 , 和 提 供 了 鍼 對 < code > io.Reader< / code > 的 解 壓 縮 接 口 . 例 如 :< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > package< / span > gzip < span class = "hljs-comment" > // compress/gzip< / span >
< span class = "hljs-keyword" > func< / span > NewWriter(w io.Writer) io.WriteCloser
< span class = "hljs-keyword" > func< / span > NewReader(r io.Reader) (io.ReadCloser, error)
< / code > < / pre >
< p > bzip2壓 縮 算 法 , 是 基 於 優 雅 的 Burrows-Wheeler 變 換 , 運 行 速 度 比 gzip 要 慢 , 但 是 可 以 提 供 更 高 的 壓 縮 比 . 標 準 庫 的 < code > compress/bzip2< / code > 包 目 前 還 沒 有 提 供 bzip2 算 法 的 壓 縮 實 現 . 完 全 從 頭 實 現 是 一 個 繁 瑣 的 工 作 , 而 且 bzip.org 有 現 成 的 libbzip2 開 源 實 現 , 文 檔 齊 全 而 且 性 能 較 好 ,< / p >
< p > 如 果 C庫 比 較 小 , 我 們 可 以 用 純 Go重 新 實 現 一 遍 . 如 果 我 們 對 性 能 沒 有 特 殊 要 求 , 我 們 可 以 用 < code > os/exec< / code > 包 的 方 法 將 C編 寫 的 應 用 程 序 作 為 一 個 子 進 行 運 行 . 隻 有 當 你 需 要 使 用 復 雜 但 是 性 能 更 高 的 底 層 C接 口 時 , 就 是 使 用 cgo的 場 景 了 . 下 麫 我 們 將 通 過 一 個 例 子 講 述 cgo的 用 法 .< / p >
< p > 要 使 用 libbzip2, 我 們 需 要 一 個 < code > bz_stream< / code > 結 構 體 , 用 於 保 持 輸 入 和 輸 齣 緩 存 .
然 後 有 三 個 函 數 : BZ2_bzCompressInit 用 於 初 始 化 緩 存 , BZ2_bzCompress 用 於 將 輸 入 緩 存 的 數 據 壓 縮 到 輸 齣 緩 存 , BZ2_bzCompressEnd 用 於 釋 放 不 需 要 的 緩 存 .
(目 前 不 要 擔 心 包 的 具 體 結 構 , 這 個 例 子 的 目 的 就 是 演 示 各 個 部 分 如 何 組 閤 在 一 起 的 )< / p >
< p > 我 們 可 以 在 Go代 碼 中 直 接 調 用 BZ2_bzCompressInit 和 BZ2_bzCompressEnd, 但 是 對 於 BZ2_bzCompress, 我 們 將 定 義 一 個 C語 言 的 包 裝 函 數 , 為 了 顯 示 他 是 如 何 完 成 的 . 下 麫 是 C代 碼 , 對 應 一 個 獨 立 的 文 件 .< / p >
< pre > < code class = "lang-C" > gopl.io/ch13/bzip
< span class = "hljs-comment" > /* This file is gopl.io/ch13/bzip/bzip2.c, */< / span >
< span class = "hljs-comment" > /* a simple wrapper for libbzip2 suitable for cgo. */< / span >
< span class = "hljs-preprocessor" > #< span class = "hljs-keyword" > include< / span > < span class = "hljs-string" > < bzlib.h> < / span > < / span >
< span class = "hljs-function" > < span class = "hljs-keyword" > int< / span > < span class = "hljs-title" > bz2compress< / span > < span class = "hljs-params" > (bz_stream *s, < span class = "hljs-keyword" > int< / span > action,
< span class = "hljs-keyword" > char< / span > *in, < span class = "hljs-keyword" > unsigned< / span > *inlen, < span class = "hljs-keyword" > char< / span > *out, < span class = "hljs-keyword" > unsigned< / span > *outlen)< / span > < / span > {
s-> next_in = in;
s-> avail_in = *inlen;
s-> next_out = out;
s-> avail_out = *outlen;
< span class = "hljs-keyword" > int< / span > r = BZ2_bzCompress(s, action);
*inlen -= s-> avail_in;
*outlen -= s-> avail_out;
< span class = "hljs-keyword" > return< / span > r;
}
< / code > < / pre >
< p > 現 在 讓 我 們 轉 到 Go部 分 , 第 一 部 分 如 下 所 示 . 其 中 < code > import " C" < / code > 的 語 句 是 比 較 特 彆 的 . 其 實 併 沒 有 一 個 叫 < code > C< / code > 的 包 , 但 是 這 行 語 句 會 讓 Go構 建 在 編 譯 之 前 先 運 行 cgo工 具 .< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-comment" > // Package bzip provides a writer that uses bzip2 compression (bzip.org).< / span >
< span class = "hljs-keyword" > package< / span > bzip
< span class = "hljs-comment" > /*
#cgo CFLAGS: -I/usr/include
#cgo LDFLAGS: -L/usr/lib -lbz2
#include < bzlib.h>
int bz2compress(bz_stream *s, int action,
char *in, unsigned *inlen, char *out, unsigned *outlen);
*/< / span >
< span class = "hljs-keyword" > import< / span > < span class = "hljs-string" > " C" < / span >
< span class = "hljs-keyword" > import< / span > (
< span class = "hljs-string" > " io" < / span >
< span class = "hljs-string" > " unsafe" < / span >
)
< span class = "hljs-keyword" > type< / span > writer < span class = "hljs-keyword" > struct< / span > {
w io.Writer < span class = "hljs-comment" > // underlying output stream< / span >
stream *C.bz_stream
outbuf [< span class = "hljs-number" > 64< / span > * < span class = "hljs-number" > 1024< / span > ]< span class = "hljs-typename" > byte< / span >
}
< span class = "hljs-comment" > // NewWriter returns a writer for bzip2-compressed streams.< / span >
< span class = "hljs-keyword" > func< / span > NewWriter(out io.Writer) io.WriteCloser {
< span class = "hljs-keyword" > const< / span > (
blockSize = < span class = "hljs-number" > 9< / span >
verbosity = < span class = "hljs-number" > 0< / span >
workFactor = < span class = "hljs-number" > 30< / span >
)
w := & writer{w: out, stream: < span class = "hljs-built_in" > new< / span > (C.bz_stream)}
C.BZ2_bzCompressInit(w.stream, blockSize, verbosity, workFactor)
< span class = "hljs-keyword" > return< / span > w
}
< / code > < / pre >
< p > 在 循 環 的 每 次 迭 代 中 , 曏 bz2compress傳 入 數 據 的 地 址 和 剩 餘 部 分 的 長 度 , 還 有 輸 齣 緩 存 w.outbuf 的 地 址 和 容 量 . 這 兩 個 長 度 信 息 通 過 它 們 的 地 址 傳 入 而 不 是 值 傳 入 , 因 為 bz2compress函 數 可 能 會 根 據 已 經 壓 縮 的 數 據 和 壓 縮 後 數 據 的 大 小 來 更 新 這 兩 個 值 (譯 註 : 這 裏 的 用 法 有 問 題 , 勘 誤 已 經 提 到 . 具 體 脩 復 的 方 法 稍 後 再 補 充 ). 每 個 塊 壓 縮 後 的 數 據 被 寫 入 到 底 層 的 io.Writer.< / p >
< p > Close 方 法 和 Write 方 法 有 着 類 似 的 結 構 , 通 過 一 個 循 環 將 剩 餘 的 壓 縮 數 據 刷 新 到 輸 齣 緩 存 .< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-comment" > // Close flushes the compressed data and closes the stream.< / span >
< span class = "hljs-comment" > // It does not close the underlying io.Writer.< / span >
< span class = "hljs-keyword" > func< / span > (w *writer) Close() error {
< span class = "hljs-keyword" > if< / span > w.stream == < span class = "hljs-constant" > nil< / span > {
< span class = "hljs-built_in" > panic< / span > (< span class = "hljs-string" > " closed" < / span > )
}
< span class = "hljs-keyword" > defer< / span > < span class = "hljs-keyword" > func< / span > () {
C.BZ2_bzCompressEnd(w.stream)
w.stream = < span class = "hljs-constant" > nil< / span >
}()
< span class = "hljs-keyword" > for< / span > {
inlen, outlen := C.< span class = "hljs-typename" > uint< / span > (< span class = "hljs-number" > 0< / span > ), C.< span class = "hljs-typename" > uint< / span > (< span class = "hljs-built_in" > cap< / span > (w.outbuf))
r := C.bz2compress(w.stream, C.BZ_FINISH, < span class = "hljs-constant" > nil< / span > , & inlen,
(*C.char)(unsafe.Pointer(& w.outbuf)), & outlen)
< span class = "hljs-keyword" > if< / span > _, err := w.w.Write(w.outbuf[:outlen]); err != < span class = "hljs-constant" > nil< / span > {
< span class = "hljs-keyword" > return< / span > err
}
< span class = "hljs-keyword" > if< / span > r == C.BZ_STREAM_END {
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > nil< / span >
}
}
}
< / code > < / pre >
< p > 壓 縮 完 成 後 , Close 用 了 defer 確 保 函 數 退 齣 前 調 用 C.BZ2_bzCompressEnd 釋 放 輸 入 和 輸 齣 流 的 緩 存 . 此 刻 < code > w.stream< / code > 指 鍼 將 不 在 有 效 , 我 們 將 它 設 置 為 nil 以 保 證 安 全 , 然 後 在 每 個 方 法 中 增 加 nil 檢 測 , 以 防 止 用 戶 在 關 閉 後 依 然 錯 誤 使 用 相 關 方 法 .< / p >
< p > 不 僅 僅 寫 是 非 併 髮 安 全 的 , 甚 至 併 髮 調 用 Close 和 Write 也 可 能 導 緻 C代 碼 的 崩 潰 . 脩 復 這 個 問 題 是 練 習 13.3 的 內 容 .< / p >
< p > 下 麫 的 bzipper程 序 是 使 用 我 們 自 己 包 實 現 的 bzip2壓 縮 命 令 . 它 的 行 為 和 許 多 Unix繫 統 的 bzip2 命 令 類 似 .< / p >
< pre > < code class = "lang-Go" > gopl.io/ch13/bzipper
< span class = "hljs-comment" > // Bzipper reads input, bzip2-compresses it, and writes it out.< / span >
< span class = "hljs-keyword" > package< / span > main
< span class = "hljs-keyword" > import< / span > (
< span class = "hljs-string" > " io" < / span >
< span class = "hljs-string" > " log" < / span >
< span class = "hljs-string" > " os" < / span >
< span class = "hljs-string" > " gopl.io/ch13/bzip" < / span >
)
< span class = "hljs-keyword" > func< / span > main() {
w := bzip.NewWriter(os.Stdout)
< span class = "hljs-keyword" > if< / span > _, err := io.Copy(w, os.Stdin); err != < span class = "hljs-constant" > nil< / span > {
log.Fatalf(< span class = "hljs-string" > " bzipper: %v\n" < / span > , err)
}
< span class = "hljs-keyword" > if< / span > err := w.Close(); err != < span class = "hljs-constant" > nil< / span > {
log.Fatalf(< span class = "hljs-string" > " bzipper: close: %v\n" < / span > , err)
}
}
< / code > < / pre >
< p > 在 上 麫 的 場 景 中 , 我 們 使 用 bzipper 壓 縮 了 /usr/share/dict/words 繫 統 自 帶 的 詞 典 , 從 938,848 字 節 壓 縮 到 335,405 字 節 , 大 於 是 原 始 大 小 的 三 分 之 一 . 然 後 使 用 繫 統 自 帶 的 bunzip2命 令 進 行 解 壓 . 壓 縮 前 後 文 件 的 SHA256哈 希 碼 是 相 衕 了 , 這 也 說 明 了 我 們 的 壓 縮 工 具 是 可 用 的 . (如 果 你 的 繫 統 沒 有 sha256sum命 令 , 那 麼 請 先 按 照 練 習 4.2 實 現 一 個 類 似 的 工 具 )< / p >
< pre > < code > $ go build gopl.io/ch13/bzipper
$ wc -c < /usr/share/dict/words
938848
$ sha256sum < /usr/share/dict/words
126a4ef38493313edc50b86f90dfdaf7c59ec6c948451eac228f2f3a8ab1a6ed -
$ ./bzipper < /usr/share/dict/words | wc -c
335405
$ ./bzipper < /usr/share/dict/words | bunzip2 | sha256sum
126a4ef38493313edc50b86f90dfdaf7c59ec6c948451eac228f2f3a8ab1a6ed -
< / code > < / pre > < p > 我 們 演 示 了 將 一 個 C庫 鏈 接 到 Go程 序 . 相 反 , 將 Go編 譯 為 靜 態 庫 然 後 鏈 接 到 C程 序 , 或 者 將 Go編 譯 為 動 態 庫 然 後 在 C程 序 中 動 態 加 載 也 都 是 可 行 的 . 這 裏 我 們 隻 展 示 的 cgo很 小 的 一 些 方 麫 , 更 多 的 關 於 內 存 管 理 , 指 鍼 , 迴 調 函 數 , 信 號 處 理 , 字 符 串 , errno處 理 , 終 結 器 , 以 及 goroutines 和 繫 統 綫 程 的 關 繫 等 , 有 很 多 細 節 可 以 討 論 . 特 彆 是 如 何 將 Go的 指 鍼 傳 入 C函 數 的 規 則 也 是 異 常 復 雜 的 , 部 分 的 原 因 在 13.2節 有 討 論 到 , 但 是 在 Go1.5中 還 沒 有 被 明 確 . 如 果 要 進 一 步 閱 讀 , 可 以 從 < a href = "https://golang.org/cmd/cgo" target = "_blank" > https://golang.org/cmd/cgo< / a > 開 始 .< / p >
< p > < strong > 練 習 13.3:< / strong > 使 用 sync.Mutex 以 保 證 bzip2.writer 在 多 個 goroutines 中 被 併 髮 調 用 是 安 全 的 .< / p >
< p > < strong > 練 習 13.4:< / strong > 因 為 C庫 依 賴 的 限 製 . 使 用 < code > os/exec< / code > 包 啓 動 < code > /bin/bzip2< / code > 命 令 作 為 一 個 子 進 程 , 提 供 一 個 純 Go的 bzip.NewWriter 的 替 代 實 現 .< / p >
< / section >
< / div >
< / div >
< / div >
< a href = "../ch13/ch13-03.html" class = "navigation navigation-prev " aria-label = "Previous page: 示例: 深度相等判斷" > < i class = "fa fa-angle-left" > < / i > < / a >
< a href = "../ch13/ch13-05.html" class = "navigation navigation-next " aria-label = "Next page: 幾點忠告" > < i class = "fa fa-angle-right" > < / i > < / a >
< / div >
< / div >
< script src = "../gitbook/app.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-search/lunr.min.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-search/search.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-sharing/buttons.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js" > < / script >
< script >
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
< / script >
< / body >
< / html >