C 与 Go 对照
同一个程序分别用 C 和 Go 编写,取自本课程的课时。
文件与首次运行
C 头文件用于声明接口,源文件会经过编译和链接。Go 中同一目录下的同级文件共享一个包,但不会以文本形式包含在一起。每个文件都声明自己的导入项。这个运行器会编译一个扁平的 main 包,其中恰好有一个 main 函数,且没有外部依赖。
C1#include <stdio.h>2int main(void) { puts("ready"); return 0; }Go1package main2import ("fmt")3 4func main(){fmt.Println("Go", 3)}if 与作用域内的名称
C 有条件运算符 ?:,Go 没有。你可以在 if/else 中赋值,或在每个分支中返回结果。C 的 switch 通常会继续执行后续分支,除非用终止语句结束;Go 的 case 默认不会继续执行后续分支。保留业务条件的含义,不要机械地照搬标点。
C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stdarg.h>5int main(void) {6 int score = 8;7 const char *label = score >= 10 ? "high" : score >= 5 ? "mid" : "low";8 puts(label);9 return 0;10}Go1package main2import ("fmt")3 4func main(){if n := len("go"); n > 1 { fmt.Println("long") } else { fmt.Println("short") }}数组是值
C 数组表达式通常会退化为指针,在函数边界处丢失长度信息。Go 数组的类型中包含长度,赋值时会进行复制。Go 切片携带对底层存储的视图;传递切片时复制的是切片描述符,而不是元素。边界检查并不意味着拥有独立的数据。
C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stdarg.h>5int main(void) {6 int values[] = {4, 8, 12};7 printf("%d\n", values[2]);8 return 0;9}Go1package main2import ("fmt")3 4func main(){a:=[2]int{3,5}5b:=a6b[0]=97fmt.Println(a[0],b[0])}带类型的函数约定
C 函数会声明结果和参数的类型;Go 则将结果类型放在参数列表之后。两者都按值传递参数。传递指针值可以修改指针指向的对象,但重新绑定局部指针不会替换调用方的指针。Go 切片也需要区分描述符和数据。
C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stdarg.h>5int twice(int value) { value *= 2; return value; }6int main(void) {7 int x = 3; int y = twice(x);8 printf("%d %d\n", x, y);9 return 0;10}Go1package main2import ("fmt")3func IsEven(n int) bool { return n%2==0 }4func main(){fmt.Println(IsEven(6))}结构体值
C 使用结构体和函数来表示数据,而不是内置的类系统。Go 结构体也包含字段,但可以通过接收者声明方法。Go 没有类继承,也不会自动调用构造函数。工厂函数可以建立不变量,并返回值或指针。
C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stdarg.h>5struct Parcel { int weight; };6int main(void) {7 struct Parcel *a = malloc(sizeof *a);8 if (a == NULL) return 1;9 a->weight = 2; struct Parcel *b = a; b->weight = 5;10 printf("%d\n", a->weight);11 free(a);12 return 0;13}Go1package main2import ("fmt")3type Label struct { Text string }4func main(){a:=Label{Text:"old"}5b:=a6b.Text="new"7fmt.Println(a.Text,b.Text)}错误是返回值
C 通常通过状态码、输出参数或 errno 报告失败。Go 将普通错误作为返回值,通常与结果一起返回。使用结果前,请先检查 err。对于转换失败或业务操作被拒绝的情况,panic 并不是常规的替代方案。
C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stdarg.h>5int nonnegative(int value, int *out) { if (value < 0) return 0; *out = value; return 1; }6int main(void) {7 int result = 0;8 int ok = nonnegative(-2, &result);9 printf("%d %d\n", ok, result);10 return 0;11}Go1package main2import ("fmt";"errors")3func Positive(n int)(int,error) { if n<=0{return 0,errors.New("not positive")};return n,nil }4func main(){_,e:=Positive(-2)5fmt.Println(e!=nil)}
课程大纲
9 章48 节课1 个项目48 个挑战99 道测验题常见问题
面向 C 开发者的 Go 适合谁学习?
适合已经会写 C、想学 Go 的人。每节课都从你熟悉的 C 代码出发,展示 Go 如何实现同样的事情,所以你可以跳过入门基础。
需要先会 Go 吗?
不需要。课程从你的第一个 Go 程序开始。你需要掌握 C 基础:变量、条件、循环、函数和集合。
面向 C 开发者的 Go 有多少节课?
面向 C 开发者的 Go 包含 48 节互动课,你可以按自己的节奏完成。
面向 C 开发者的 Go 包含哪些内容?
本课程分为以下几章:值与表达式, 控制流, 集合, 函数与指针, and more。
面向 C 开发者的 Go 免费吗?
是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。