gopl-zh.github.com/ch13/ch13-04.html
2015-12-16 10:56:29 +08:00

2241 lines
75 KiB
HTML

<!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>
<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 (中国标准时间)">
<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-&#x901A;&#x904E;cgo&#x8ABF;&#x7528;c&#x4EE3;&#x78BC;">13.4. &#x901A;&#x904E;cgo&#x8ABF;&#x7528;C&#x4EE3;&#x78BC;</h2>
<p>Go&#x7A0B;&#x5E8F;&#x53EF;&#x80FD;&#x6703;&#x9047;&#x5230;&#x8981;&#x8A2A;&#x554F;C&#x8A9E;&#x8A00;&#x7684;&#x67D0;&#x4E9B;&#x786C;&#x4EF6;&#x9A45;&#x52D5;&#x7684;&#x5834;&#x666F;, &#x6216;&#x8005;&#x662F;&#x5F9E;&#x4E00;&#x500B;C++&#x5BE6;&#x73FE;&#x7684;&#x5D4C;&#x5165;&#x5F0F;&#x6578;&#x64DA;&#x5EAB;&#x67FB;&#x8A62;&#x8A18;&#x5F54;&#x7684;&#x5834;&#x666F;, &#x6216;&#x8005;&#x662F;&#x4F7F;&#x7528;Fortran&#x5BE6;&#x73FE;&#x7684;&#x4E00;&#x4E9B;&#x7DAB;&#x6027;&#x4EE3;&#x6578;&#x5EAB;&#x7684;&#x5834;&#x666F;. C&#x4F5C;&#x70BA;&#x4E00;&#x500B;&#x901A;&#x7528;&#x8A9E;&#x8A00;, &#x5F88;&#x591A;&#x5EAB;&#x6703;&#x9078;&#x64C7;&#x63D0;&#x4F9B;&#x4E00;&#x500B;C&#x517C;&#x5BB9;&#x7684;API, &#x7136;&#x5F8C;&#x7528;&#x5176;&#x4ED6;&#x8A9E;&#x8A00;&#x5BE6;&#x73FE;.</p>
<p>&#x5728;&#x672C;&#x7BC0;&#x4E2D;, &#x6211;&#x5011;&#x5C07;&#x69CB;&#x5EFA;&#x4E00;&#x500B;&#x7C21;&#x6613;&#x7684;&#x6578;&#x64DA;&#x58D3;&#x7E2E;&#x7A0B;&#x5E8F;, &#x901A;&#x904E;&#x4F7F;&#x7528;&#x4E00;&#x500B;Go&#x8A9E;&#x8A00;&#x81EA;&#x5E36;&#x7684;&#x53EB;cgo&#x7684;&#x7528;&#x65BC;&#x652F;&#x63F4;C&#x8A9E;&#x8A00;&#x51FD;&#x6578;&#x8ABF;&#x7528;&#x7684;&#x5DE5;&#x5177;. &#x9019;&#x985E;&#x5DE5;&#x5177;&#x88AB;&#x7A31;&#x70BA;&#x5916;&#x570D;&#x51FD;&#x6578;&#x63A5;&#x53E3;(ffi), &#x4F75;&#x4E14;cgo&#x4E5F;&#x4E0D;&#x662F;Go&#x4E2D;&#x552F;&#x4E00;&#x7684;&#x985E;&#x4F3C;&#x5DE5;&#x5177;. SWIG(swig.org) &#x662F;&#x985E;&#x4F3C;&#x7684;&#x53E6;&#x4E00;&#x500B;&#x88AB;&#x5EE3;&#x6C3E;&#x4F7F;&#x7528;&#x7684;&#x5DE5;&#x5177;, &#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x5F88;&#x591A;&#x5FA9;&#x96DC;&#x7279;&#x6027;&#x4EE5;&#x652F;&#x63F4;C++&#x7684;&#x96C6;&#x6210;, &#x4F46; SWIG &#x4E0D;&#x662F;&#x9019;&#x88CF;&#x8981;&#x8A0E;&#x8AD6;&#x7684;&#x4E3B;&#x984C;.</p>
<p>&#x5728;&#x6A19;&#x6E96;&#x5EAB;&#x7684; <code>compress/...</code> &#x5B50;&#x76EE;&#x5F54;&#x6709;&#x5F88;&#x591A;&#x6D41;&#x884C;&#x7684;&#x58D3;&#x7E2E;&#x7B97;&#x6CD5;&#x7684;&#x7DE8;&#x78BC;&#x548C;&#x89E3;&#x78BC;&#x5BE6;&#x73FE;, &#x5305;&#x62EC;LZW&#x58D3;&#x7E2E;&#x7B97;&#x6CD5;(Unix&#x7684;compress&#x547D;&#x4EE4;&#x7528;&#x7684;&#x7B97;&#x6CD5;)&#x548C;DEFLATE&#x58D3;&#x7E2E;&#x7B97;&#x6CD5;(GNU gzip&#x547D;&#x4EE4;&#x7528;&#x7684;&#x7B97;&#x6CD5;). &#x9019;&#x4E9B;&#x5305;&#x7684;API&#x7684;&#x7D30;&#x7BC0;&#x6709;&#x4E9B;&#x5DEE;&#x7570;, &#x4F46;&#x662F;&#x5B83;&#x5011;&#x90FD;&#x63D0;&#x4F9B;&#x4E86;&#x937C;&#x5C0D; <code>io.Writer</code> &#x7684;&#x58D3;&#x7E2E;&#x63A5;&#x53E3;, &#x548C;&#x63D0;&#x4F9B;&#x4E86;&#x937C;&#x5C0D; <code>io.Reader</code> &#x7684;&#x89E3;&#x58D3;&#x7E2E;&#x63A5;&#x53E3;. &#x4F8B;&#x5982;:</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&#x58D3;&#x7E2E;&#x7B97;&#x6CD5;, &#x662F;&#x57FA;&#x65BC;&#x512A;&#x96C5;&#x7684; Burrows-Wheeler &#x8B8A;&#x63DB;, &#x904B;&#x884C;&#x901F;&#x5EA6;&#x6BD4; gzip &#x8981;&#x6162;, &#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x66F4;&#x9AD8;&#x7684;&#x58D3;&#x7E2E;&#x6BD4;. &#x6A19;&#x6E96;&#x5EAB;&#x7684; <code>compress/bzip2</code> &#x5305;&#x76EE;&#x524D;&#x9084;&#x6C92;&#x6709;&#x63D0;&#x4F9B; bzip2 &#x7B97;&#x6CD5;&#x7684;&#x58D3;&#x7E2E;&#x5BE6;&#x73FE;. &#x5B8C;&#x5168;&#x5F9E;&#x982D;&#x5BE6;&#x73FE;&#x662F;&#x4E00;&#x500B;&#x7E41;&#x7463;&#x7684;&#x5DE5;&#x4F5C;, &#x800C;&#x4E14; bzip.org &#x6709;&#x73FE;&#x6210;&#x7684; libbzip2 &#x958B;&#x6E90;&#x5BE6;&#x73FE;, &#x6587;&#x6A94;&#x9F4A;&#x5168;&#x800C;&#x4E14;&#x6027;&#x80FD;&#x8F03;&#x597D;,</p>
<p>&#x5982;&#x679C;C&#x5EAB;&#x6BD4;&#x8F03;&#x5C0F;, &#x6211;&#x5011;&#x53EF;&#x4EE5;&#x7528;&#x7D14;Go&#x91CD;&#x65B0;&#x5BE6;&#x73FE;&#x4E00;&#x904D;. &#x5982;&#x679C;&#x6211;&#x5011;&#x5C0D;&#x6027;&#x80FD;&#x6C92;&#x6709;&#x7279;&#x6B8A;&#x8981;&#x6C42;, &#x6211;&#x5011;&#x53EF;&#x4EE5;&#x7528; <code>os/exec</code> &#x5305;&#x7684;&#x65B9;&#x6CD5;&#x5C07;C&#x7DE8;&#x5BEB;&#x7684;&#x61C9;&#x7528;&#x7A0B;&#x5E8F;&#x4F5C;&#x70BA;&#x4E00;&#x500B;&#x5B50;&#x9032;&#x884C;&#x904B;&#x884C;. &#x96BB;&#x6709;&#x7576;&#x4F60;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5FA9;&#x96DC;&#x4F46;&#x662F;&#x6027;&#x80FD;&#x66F4;&#x9AD8;&#x7684;&#x5E95;&#x5C64;C&#x63A5;&#x53E3;&#x6642;, &#x5C31;&#x662F;&#x4F7F;&#x7528;cgo&#x7684;&#x5834;&#x666F;&#x4E86;. &#x4E0B;&#x9EAB;&#x6211;&#x5011;&#x5C07;&#x901A;&#x904E;&#x4E00;&#x500B;&#x4F8B;&#x5B50;&#x8B1B;&#x8FF0;cgo&#x7684;&#x7528;&#x6CD5;.</p>
<p>&#x8981;&#x4F7F;&#x7528; libbzip2, &#x6211;&#x5011;&#x9700;&#x8981;&#x4E00;&#x500B; <code>bz_stream</code> &#x7D50;&#x69CB;&#x9AD4;, &#x7528;&#x65BC;&#x4FDD;&#x6301;&#x8F38;&#x5165;&#x548C;&#x8F38;&#x9F63;&#x7DE9;&#x5B58;.
&#x7136;&#x5F8C;&#x6709;&#x4E09;&#x500B;&#x51FD;&#x6578;: BZ2_bzCompressInit &#x7528;&#x65BC;&#x521D;&#x59CB;&#x5316;&#x7DE9;&#x5B58;, BZ2_bzCompress &#x7528;&#x65BC;&#x5C07;&#x8F38;&#x5165;&#x7DE9;&#x5B58;&#x7684;&#x6578;&#x64DA;&#x58D3;&#x7E2E;&#x5230;&#x8F38;&#x9F63;&#x7DE9;&#x5B58;, BZ2_bzCompressEnd &#x7528;&#x65BC;&#x91CB;&#x653E;&#x4E0D;&#x9700;&#x8981;&#x7684;&#x7DE9;&#x5B58;.
(&#x76EE;&#x524D;&#x4E0D;&#x8981;&#x64D4;&#x5FC3;&#x5305;&#x7684;&#x5177;&#x9AD4;&#x7D50;&#x69CB;, &#x9019;&#x500B;&#x4F8B;&#x5B50;&#x7684;&#x76EE;&#x7684;&#x5C31;&#x662F;&#x6F14;&#x793A;&#x5404;&#x500B;&#x90E8;&#x5206;&#x5982;&#x4F55;&#x7D44;&#x95A4;&#x5728;&#x4E00;&#x8D77;&#x7684;)</p>
<p>&#x6211;&#x5011;&#x53EF;&#x4EE5;&#x5728;Go&#x4EE3;&#x78BC;&#x4E2D;&#x76F4;&#x63A5;&#x8ABF;&#x7528; BZ2_bzCompressInit &#x548C; BZ2_bzCompressEnd, &#x4F46;&#x662F;&#x5C0D;&#x65BC; BZ2_bzCompress, &#x6211;&#x5011;&#x5C07;&#x5B9A;&#x7FA9;&#x4E00;&#x500B;C&#x8A9E;&#x8A00;&#x7684;&#x5305;&#x88DD;&#x51FD;&#x6578;, &#x70BA;&#x4E86;&#x986F;&#x793A;&#x4ED6;&#x662F;&#x5982;&#x4F55;&#x5B8C;&#x6210;&#x7684;. &#x4E0B;&#x9EAB;&#x662F;C&#x4EE3;&#x78BC;, &#x5C0D;&#x61C9;&#x4E00;&#x500B;&#x7368;&#x7ACB;&#x7684;&#x6587;&#x4EF6;.</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">&lt;bzlib.h&gt;</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-&gt;next_in = in;
s-&gt;avail_in = *inlen;
s-&gt;next_out = out;
s-&gt;avail_out = *outlen;
<span class="hljs-keyword">int</span> r = BZ2_bzCompress(s, action);
*inlen -= s-&gt;avail_in;
*outlen -= s-&gt;avail_out;
<span class="hljs-keyword">return</span> r;
}
</code></pre>
<p>&#x73FE;&#x5728;&#x8B93;&#x6211;&#x5011;&#x8F49;&#x5230;Go&#x90E8;&#x5206;, &#x7B2C;&#x4E00;&#x90E8;&#x5206;&#x5982;&#x4E0B;&#x6240;&#x793A;. &#x5176;&#x4E2D; <code>import &quot;C&quot;</code> &#x7684;&#x8A9E;&#x53E5;&#x662F;&#x6BD4;&#x8F03;&#x7279;&#x5F46;&#x7684;. &#x5176;&#x5BE6;&#x4F75;&#x6C92;&#x6709;&#x4E00;&#x500B;&#x53EB; <code>C</code> &#x7684;&#x5305;, &#x4F46;&#x662F;&#x9019;&#x884C;&#x8A9E;&#x53E5;&#x6703;&#x8B93;Go&#x69CB;&#x5EFA;&#x5728;&#x7DE8;&#x8B6F;&#x4E4B;&#x524D;&#x5148;&#x904B;&#x884C;cgo&#x5DE5;&#x5177;.</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 &lt;bzlib.h&gt;
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">&quot;C&quot;</span>
<span class="hljs-keyword">import</span> (
<span class="hljs-string">&quot;io&quot;</span>
<span class="hljs-string">&quot;unsafe&quot;</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 := &amp;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>&#x5728;&#x5FAA;&#x74B0;&#x7684;&#x6BCF;&#x6B21;&#x8FED;&#x4EE3;&#x4E2D;, &#x66CF;bz2compress&#x50B3;&#x5165;&#x6578;&#x64DA;&#x7684;&#x5730;&#x5740;&#x548C;&#x5269;&#x9918;&#x90E8;&#x5206;&#x7684;&#x9577;&#x5EA6;, &#x9084;&#x6709;&#x8F38;&#x9F63;&#x7DE9;&#x5B58; w.outbuf &#x7684;&#x5730;&#x5740;&#x548C;&#x5BB9;&#x91CF;. &#x9019;&#x5169;&#x500B;&#x9577;&#x5EA6;&#x4FE1;&#x606F;&#x901A;&#x904E;&#x5B83;&#x5011;&#x7684;&#x5730;&#x5740;&#x50B3;&#x5165;&#x800C;&#x4E0D;&#x662F;&#x503C;&#x50B3;&#x5165;, &#x56E0;&#x70BA;bz2compress&#x51FD;&#x6578;&#x53EF;&#x80FD;&#x6703;&#x6839;&#x64DA;&#x5DF2;&#x7D93;&#x58D3;&#x7E2E;&#x7684;&#x6578;&#x64DA;&#x548C;&#x58D3;&#x7E2E;&#x5F8C;&#x6578;&#x64DA;&#x7684;&#x5927;&#x5C0F;&#x4F86;&#x66F4;&#x65B0;&#x9019;&#x5169;&#x500B;&#x503C;(&#x8B6F;&#x8A3B;: &#x9019;&#x88CF;&#x7684;&#x7528;&#x6CD5;&#x6709;&#x554F;&#x984C;, &#x52D8;&#x8AA4;&#x5DF2;&#x7D93;&#x63D0;&#x5230;. &#x5177;&#x9AD4;&#x8129;&#x5FA9;&#x7684;&#x65B9;&#x6CD5;&#x7A0D;&#x5F8C;&#x518D;&#x88DC;&#x5145;). &#x6BCF;&#x500B;&#x584A;&#x58D3;&#x7E2E;&#x5F8C;&#x7684;&#x6578;&#x64DA;&#x88AB;&#x5BEB;&#x5165;&#x5230;&#x5E95;&#x5C64;&#x7684; io.Writer.</p>
<p>Close &#x65B9;&#x6CD5;&#x548C; Write &#x65B9;&#x6CD5;&#x6709;&#x7740;&#x985E;&#x4F3C;&#x7684;&#x7D50;&#x69CB;, &#x901A;&#x904E;&#x4E00;&#x500B;&#x5FAA;&#x74B0;&#x5C07;&#x5269;&#x9918;&#x7684;&#x58D3;&#x7E2E;&#x6578;&#x64DA;&#x5237;&#x65B0;&#x5230;&#x8F38;&#x9F63;&#x7DE9;&#x5B58;.</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">&quot;closed&quot;</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>, &amp;inlen,
(*C.char)(unsafe.Pointer(&amp;w.outbuf)), &amp;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>&#x58D3;&#x7E2E;&#x5B8C;&#x6210;&#x5F8C;, Close &#x7528;&#x4E86; defer &#x78BA;&#x4FDD;&#x51FD;&#x6578;&#x9000;&#x9F63;&#x524D;&#x8ABF;&#x7528; C.BZ2_bzCompressEnd &#x91CB;&#x653E;&#x8F38;&#x5165;&#x548C;&#x8F38;&#x9F63;&#x6D41;&#x7684;&#x7DE9;&#x5B58;. &#x6B64;&#x523B; <code>w.stream</code> &#x6307;&#x937C;&#x5C07;&#x4E0D;&#x5728;&#x6709;&#x6548;, &#x6211;&#x5011;&#x5C07;&#x5B83;&#x8A2D;&#x7F6E;&#x70BA; nil &#x4EE5;&#x4FDD;&#x8B49;&#x5B89;&#x5168;, &#x7136;&#x5F8C;&#x5728;&#x6BCF;&#x500B;&#x65B9;&#x6CD5;&#x4E2D;&#x589E;&#x52A0; nil &#x6AA2;&#x6E2C;, &#x4EE5;&#x9632;&#x6B62;&#x7528;&#x6236;&#x5728;&#x95DC;&#x9589;&#x5F8C;&#x4F9D;&#x7136;&#x932F;&#x8AA4;&#x4F7F;&#x7528;&#x76F8;&#x95DC;&#x65B9;&#x6CD5;.</p>
<p>&#x4E0D;&#x50C5;&#x50C5;&#x5BEB;&#x662F;&#x975E;&#x4F75;&#x9AEE;&#x5B89;&#x5168;&#x7684;, &#x751A;&#x81F3;&#x4F75;&#x9AEE;&#x8ABF;&#x7528; Close &#x548C; Write &#x4E5F;&#x53EF;&#x80FD;&#x5C0E;&#x7DFB;C&#x4EE3;&#x78BC;&#x7684;&#x5D29;&#x6F70;. &#x8129;&#x5FA9;&#x9019;&#x500B;&#x554F;&#x984C;&#x662F; &#x7DF4;&#x7FD2;13.3 &#x7684;&#x5167;&#x5BB9;.</p>
<p>&#x4E0B;&#x9EAB;&#x7684;bzipper&#x7A0B;&#x5E8F;&#x662F;&#x4F7F;&#x7528;&#x6211;&#x5011;&#x81EA;&#x5DF1;&#x5305;&#x5BE6;&#x73FE;&#x7684;bzip2&#x58D3;&#x7E2E;&#x547D;&#x4EE4;. &#x5B83;&#x7684;&#x884C;&#x70BA;&#x548C;&#x8A31;&#x591A;Unix&#x7E6B;&#x7D71;&#x7684; bzip2 &#x547D;&#x4EE4;&#x985E;&#x4F3C;.</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">&quot;io&quot;</span>
<span class="hljs-string">&quot;log&quot;</span>
<span class="hljs-string">&quot;os&quot;</span>
<span class="hljs-string">&quot;gopl.io/ch13/bzip&quot;</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">&quot;bzipper: %v\n&quot;</span>, err)
}
<span class="hljs-keyword">if</span> err := w.Close(); err != <span class="hljs-constant">nil</span> {
log.Fatalf(<span class="hljs-string">&quot;bzipper: close: %v\n&quot;</span>, err)
}
}
</code></pre>
<p>&#x5728;&#x4E0A;&#x9EAB;&#x7684;&#x5834;&#x666F;&#x4E2D;, &#x6211;&#x5011;&#x4F7F;&#x7528; bzipper &#x58D3;&#x7E2E;&#x4E86; /usr/share/dict/words &#x7E6B;&#x7D71;&#x81EA;&#x5E36;&#x7684;&#x8A5E;&#x5178;, &#x5F9E; 938,848 &#x5B57;&#x7BC0;&#x58D3;&#x7E2E;&#x5230; 335,405 &#x5B57;&#x7BC0;, &#x5927;&#x65BC;&#x662F;&#x539F;&#x59CB;&#x5927;&#x5C0F;&#x7684;&#x4E09;&#x5206;&#x4E4B;&#x4E00;. &#x7136;&#x5F8C;&#x4F7F;&#x7528;&#x7E6B;&#x7D71;&#x81EA;&#x5E36;&#x7684;bunzip2&#x547D;&#x4EE4;&#x9032;&#x884C;&#x89E3;&#x58D3;. &#x58D3;&#x7E2E;&#x524D;&#x5F8C;&#x6587;&#x4EF6;&#x7684;SHA256&#x54C8;&#x5E0C;&#x78BC;&#x662F;&#x76F8;&#x8855;&#x4E86;, &#x9019;&#x4E5F;&#x8AAA;&#x660E;&#x4E86;&#x6211;&#x5011;&#x7684;&#x58D3;&#x7E2E;&#x5DE5;&#x5177;&#x662F;&#x53EF;&#x7528;&#x7684;. (&#x5982;&#x679C;&#x4F60;&#x7684;&#x7E6B;&#x7D71;&#x6C92;&#x6709;sha256sum&#x547D;&#x4EE4;, &#x90A3;&#x9EBC;&#x8ACB;&#x5148;&#x6309;&#x7167; &#x7DF4;&#x7FD2;4.2 &#x5BE6;&#x73FE;&#x4E00;&#x500B;&#x985E;&#x4F3C;&#x7684;&#x5DE5;&#x5177;)</p>
<pre><code>$ go build gopl.io/ch13/bzipper
$ wc -c &lt; /usr/share/dict/words
938848
$ sha256sum &lt; /usr/share/dict/words
126a4ef38493313edc50b86f90dfdaf7c59ec6c948451eac228f2f3a8ab1a6ed -
$ ./bzipper &lt; /usr/share/dict/words | wc -c
335405
$ ./bzipper &lt; /usr/share/dict/words | bunzip2 | sha256sum
126a4ef38493313edc50b86f90dfdaf7c59ec6c948451eac228f2f3a8ab1a6ed -
</code></pre><p>&#x6211;&#x5011;&#x6F14;&#x793A;&#x4E86;&#x5C07;&#x4E00;&#x500B;C&#x5EAB;&#x93C8;&#x63A5;&#x5230;Go&#x7A0B;&#x5E8F;. &#x76F8;&#x53CD;, &#x5C07;Go&#x7DE8;&#x8B6F;&#x70BA;&#x975C;&#x614B;&#x5EAB;&#x7136;&#x5F8C;&#x93C8;&#x63A5;&#x5230;C&#x7A0B;&#x5E8F;, &#x6216;&#x8005;&#x5C07;Go&#x7DE8;&#x8B6F;&#x70BA;&#x52D5;&#x614B;&#x5EAB;&#x7136;&#x5F8C;&#x5728;C&#x7A0B;&#x5E8F;&#x4E2D;&#x52D5;&#x614B;&#x52A0;&#x8F09;&#x4E5F;&#x90FD;&#x662F;&#x53EF;&#x884C;&#x7684;. &#x9019;&#x88CF;&#x6211;&#x5011;&#x96BB;&#x5C55;&#x793A;&#x7684;cgo&#x5F88;&#x5C0F;&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x9EAB;, &#x66F4;&#x591A;&#x7684;&#x95DC;&#x65BC;&#x5167;&#x5B58;&#x7BA1;&#x7406;, &#x6307;&#x937C;, &#x8FF4;&#x8ABF;&#x51FD;&#x6578;, &#x4FE1;&#x865F;&#x8655;&#x7406;, &#x5B57;&#x7B26;&#x4E32;, errno&#x8655;&#x7406;, &#x7D42;&#x7D50;&#x5668;, &#x4EE5;&#x53CA; goroutines &#x548C;&#x7E6B;&#x7D71;&#x7DAB;&#x7A0B;&#x7684;&#x95DC;&#x7E6B;&#x7B49;, &#x6709;&#x5F88;&#x591A;&#x7D30;&#x7BC0;&#x53EF;&#x4EE5;&#x8A0E;&#x8AD6;. &#x7279;&#x5F46;&#x662F;&#x5982;&#x4F55;&#x5C07;Go&#x7684;&#x6307;&#x937C;&#x50B3;&#x5165;C&#x51FD;&#x6578;&#x7684;&#x898F;&#x5247;&#x4E5F;&#x662F;&#x7570;&#x5E38;&#x5FA9;&#x96DC;&#x7684;, &#x90E8;&#x5206;&#x7684;&#x539F;&#x56E0;&#x5728; 13.2&#x7BC0; &#x6709;&#x8A0E;&#x8AD6;&#x5230;, &#x4F46;&#x662F;&#x5728;Go1.5&#x4E2D;&#x9084;&#x6C92;&#x6709;&#x88AB;&#x660E;&#x78BA;. &#x5982;&#x679C;&#x8981;&#x9032;&#x4E00;&#x6B65;&#x95B1;&#x8B80;, &#x53EF;&#x4EE5;&#x5F9E; <a href="https://golang.org/cmd/cgo" target="_blank">https://golang.org/cmd/cgo</a> &#x958B;&#x59CB;.</p>
<p><strong>&#x7DF4;&#x7FD2;13.3:</strong> &#x4F7F;&#x7528; sync.Mutex &#x4EE5;&#x4FDD;&#x8B49; bzip2.writer &#x5728;&#x591A;&#x500B; goroutines &#x4E2D;&#x88AB;&#x4F75;&#x9AEE;&#x8ABF;&#x7528;&#x662F;&#x5B89;&#x5168;&#x7684;.</p>
<p><strong>&#x7DF4;&#x7FD2;13.4:</strong> &#x56E0;&#x70BA;C&#x5EAB;&#x4F9D;&#x8CF4;&#x7684;&#x9650;&#x88FD;. &#x4F7F;&#x7528; <code>os/exec</code> &#x5305;&#x5553;&#x52D5; <code>/bin/bzip2</code> &#x547D;&#x4EE4;&#x4F5C;&#x70BA;&#x4E00;&#x500B;&#x5B50;&#x9032;&#x7A0B;, &#x63D0;&#x4F9B;&#x4E00;&#x500B;&#x7D14;Go&#x7684; bzip.NewWriter &#x7684;&#x66FF;&#x4EE3;&#x5BE6;&#x73FE;.</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>