Skip to content

Commit 4e1fd10

Browse files
committed
9.4
1 parent b8e14e9 commit 4e1fd10

File tree

5 files changed

+897
-4
lines changed

5 files changed

+897
-4
lines changed

src/develop/FrontEnd/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 前端
2+
3+
原来写的咋就找不到了呢~ 真服了

src/develop/Go/ImprovingGo/1_GoFinal.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,4 +983,104 @@ ch := make(chan int, 5) // 创建一个容量为 5 的有缓冲 channel
983983
result := <-resultCh // 接收异步操作的结果
984984
```
985985

986-
这些场景只是使用无缓冲和有缓冲 channel 的一部分示例。根据具体的需求,合理选择适当类型的 channel 可以提高程序的效率和可维护性,同时确保并发操作的正确性。
986+
这些场景只是使用无缓冲和有缓冲 channel 的一部分示例。根据具体的需求,合理选择适当类型的 channel 可以提高程序的效率和可维护性,同时确保并发操作的正确性。
987+
988+
## 1.9. cgo-Go调用C代码
989+
990+
在 Go 语言中,CGO 是 Go 语言提供的一种特性,用于在 Go 代码中调用 C 语言代码。通过 CGO,可以很方便地在 Go 代码中集成现有的 C 代码库,或者利用 C 语言的性能优势来编写高性能的代码片段。
991+
992+
使用 CGO 的一般流程包括以下步骤:
993+
994+
1. **编写 C 代码**:首先需要编写需要调用的 C 代码,可以是一个简单的 C 函数或者一个 C 语言库。
995+
2. **创建 C 头文件**:为了在 Go 代码中调用 C 函数,需要创建一个 C 头文件,用于声明 C 函数的原型。
996+
3. **在 Go 代码中使用 CGO**:在 Go 代码中通过 `import "C"` 来引入 CGO,然后通过 `// #cgo` 指令告诉编译器去链接 C 代码。
997+
4. **调用 C 函数**:在 Go 代码中就可以像调用普通 Go 函数一样调用 C 函数,通过 CGO 技术实现 Go 与 C 语言的互操作。
998+
999+
以下是一个更详细的示例,展示如何使用 CGO 在 Go 中调用一个简单的 C 函数来实现字符串加密和解密功能。
1000+
1001+
**1. 编写 C 代码**
1002+
1003+
首先,我们编写两个简单的 C 函数,一个用于加密字符串,另一个用于解密字符串。
1004+
1005+
```c
1006+
// encrypt.c
1007+
#include <stdio.h>
1008+
1009+
void encrypt(char* str) {
1010+
while (*str) {
1011+
*str = *str ^ 31;
1012+
str++;
1013+
}
1014+
}
1015+
1016+
// decrypt.c
1017+
#include <stdio.h>
1018+
1019+
void decrypt(char* str) {
1020+
while (*str) {
1021+
*str = *str ^ 31;
1022+
str++;
1023+
}
1024+
}
1025+
```
1026+
1027+
**2. 创建 C 头文件 `crypto.h`**
1028+
1029+
创建一个头文件 `crypto.h`,用于声明 C 函数的原型。
1030+
1031+
```c
1032+
// crypto.h
1033+
void encrypt(char* str);
1034+
void decrypt(char* str);
1035+
```
1036+
1037+
**3. 编写 Go 代码**
1038+
1039+
接下来,我们编写 Go 代码,通过 CGO 调用上述的 C 函数来加密和解密字符串。
1040+
1041+
```go
1042+
package main
1043+
1044+
/*
1045+
#cgo CFLAGS: -g -Wall
1046+
#cgo LDFLAGS: -lm
1047+
#include "crypto.h"
1048+
*/
1049+
import "C"
1050+
import "fmt"
1051+
1052+
func main() {
1053+
message := "Hello, world!"
1054+
1055+
// Encrypt the message
1056+
cMessage := C.CString(message)
1057+
defer C.free(unsafe.Pointer(cMessage))
1058+
C.encrypt(cMessage)
1059+
fmt.Printf("Encrypted message: %s\n", C.GoString(cMessage))
1060+
1061+
// Decrypt the message
1062+
C.decrypt(cMessage)
1063+
fmt.Printf("Decrypted message: %s\n", C.GoString(cMessage))
1064+
}
1065+
```
1066+
1067+
在这个示例中,Go 代码通过 `import "C"` 引入 CGO,然后使用 `// #cgo` 指令来指定编译选项。在 `main` 函数中,我们首先将 Go 字符串转换为 C 字符串,然后调用 C 函数来加密和解密字符串,最后将结果打印出来。
1068+
1069+
**4. 构建和运行程序**
1070+
1071+
在包含以上文件的目录中,可以通过以下命令构建和运行这个示例程序:
1072+
1073+
```bash
1074+
go build -o crypto
1075+
./crypto
1076+
```
1077+
1078+
程序应该输出以下内容:
1079+
1080+
```
1081+
Encrypted message: LQYYX;#rNXVX
1082+
Decrypted message: Hello, world!
1083+
```
1084+
1085+
1086+

src/develop/Go/gopl/ch12.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Sprint(x interface{}) string {
4040

4141
```
4242

43-
但是我们如何处理其它类似[]float64map[string][]string等类型呢?我们当然可以添加更多的测试分支,但是这些组合类型的数目基本是无穷的。还有如何处理类似url.Values这样的具名类型呢?即使类型分支可以识别出底层的基础类型是map[string][]string,但是它并不匹配url.Values类型,因为它们是两种不同的类型,而且switch类型分支也不可能包含每个类似url.Values的类型,这会导致对这些库的依赖。
43+
但是我们如何处理其它类似`[]float64``map[string][]string`等类型呢?我们当然可以添加更多的测试分支,但是这些组合类型的数目基本是无穷的。还有如何处理类似`url.Values`这样的具名类型呢?即使类型分支可以识别出底层的基础类型是`map[string][]string`,但是它并不匹配`url.Values`类型,因为它们是两种不同的类型,而且`switch`类型分支也不可能包含每个类似`url.Values`的类型,这会导致对这些库的依赖。
4444

4545
没有办法来检查未知类型的表示方式,我们被卡住了。这就是我们需要反射的原因。
4646

@@ -82,7 +82,7 @@ func main() {
8282
}
8383
```
8484

85-
函数`reflect.TypeOf` 接受任意的 `interface{} `类型,并以`` reflect.Type `形式返回其动态类型:
85+
函数`reflect.TypeOf` 接受任意的 `interface{} `类型,并以` reflect.Type `形式返回其动态类型:
8686

8787
```Go
8888
t := reflect.TypeOf(3) // a reflect.Type
@@ -609,4 +609,5 @@ methods.Print(new(strings.Replacer))
609609
// func (*strings.Replacer) WriteString(io.Writer, string) (int, error)
610610
```
611611

612-
![image-20240731164211191](img/ch12/image-20240731164211191.png)
612+
![image-20240731164211191](img/ch12/image-20240731164211191.png)
613+

0 commit comments

Comments
 (0)