Appearance
5 分钟教程
通过这个快速教程,你将在 5 分钟内创建并运行你的第一个 Targo 程序。
你将学到
- 创建 Targo 项目
- 编写简单的 HTTP 服务器
- 使用 Go 标准库
- 编译和运行程序
前置要求
- 已安装 Node.js 18+ 和 Go 1.22+
- 已安装 Targo CLI(参见 安装指南)
步骤 1: 创建项目
bash
# 创建新项目
npx @targo/create-targo hello-server
cd hello-server步骤 2: 绑定 Go 包
我们需要使用 Go 的 HTTP 包,先绑定它:
bash
targo bind fmt net/http这会在 .targo/std/ 目录下生成类型声明文件。
步骤 3: 编写 HTTP 服务器
创建 src/main.ts 文件:
typescript
import { Fprintf } from "fmt";
import { HandleFunc, ListenAndServe, Request, ResponseWriter } from "net/http";
// 处理器函数
function handler(w: ResponseWriter, r: Request): void {
const name = r.URL.Path.slice(1) || "World";
Fprintf(w, "Hello, %s!", name);
}
// 主函数
function main(): void {
// 注册路由
HandleFunc("/", handler);
// 启动服务器
console.log("Server starting on http://localhost:8080");
ListenAndServe(":8080", null);
}代码说明:
- 导入 Go 包: 使用标准的 ES6 import 语法导入 Go 函数
- 处理器函数: 接收 HTTP 请求并返回响应
- 主函数: 注册路由并启动服务器
步骤 4: 类型检查
在运行之前,先检查类型错误:
bash
targo check如果没有错误,你会看到:
✓ Type checking completed successfully步骤 5: 运行服务器
bash
targo run src/main.ts你会看到:
Server starting on http://localhost:8080步骤 6: 测试服务器
打开浏览器访问:
http://localhost:8080/→ 显示 "Hello, World!"http://localhost:8080/Targo→ 显示 "Hello, Targo!"http://localhost:8080/Alice→ 显示 "Hello, Alice!"
或使用 curl:
bash
curl http://localhost:8080/Targo
# 输出: Hello, Targo!完整代码
这是完整的可运行代码:
typescript
import { Fprintf } from "fmt";
import { HandleFunc, ListenAndServe, Request, ResponseWriter } from "net/http";
function handler(w: ResponseWriter, r: Request): void {
const name = r.URL.Path.slice(1) || "World";
Fprintf(w, "Hello, %s!", name);
}
function main(): void {
HandleFunc("/", handler);
console.log("Server starting on http://localhost:8080");
ListenAndServe(":8080", null);
}go
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
if name == "" {
name = "World"
}
fmt.Fprintf(w, "Hello, %s!", name)
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Server starting on http://localhost:8080")
http.ListenAndServe(":8080", nil)
}添加并发处理
让我们添加一个简单的计数器来演示并发:
typescript
import { Fprintf, Println } from "fmt";
import { HandleFunc, ListenAndServe, Request, ResponseWriter } from "net/http";
// 全局计数器
let requestCount: int = 0;
function handler(w: ResponseWriter, r: Request): void {
// 增加计数器(注意:这在生产环境中需要使用互斥锁)
requestCount++;
const name = r.URL.Path.slice(1) || "World";
Fprintf(w, "Hello, %s! (Request #%d)", name, requestCount);
// 在服务器端打印日志
Println(`Handled request for: ${name}`);
}
function main(): void {
HandleFunc("/", handler);
Println("Server starting on http://localhost:8080");
ListenAndServe(":8080", null);
}重新运行:
bash
targo run src/main.ts现在每次访问都会显示请求计数!
构建生产版本
开发完成后,构建生产版本:
bash
# 编译为 Go 代码
targo build
# 使用 Go 编译为二进制文件
go build -o server ./dist
# 运行二进制文件
./server与 TypeScript 的对比
如果你熟悉 TypeScript + Express.js,这是对比:
typescript
import express from 'express';
const app = express();
let requestCount = 0;
app.get('/:name?', (req, res) => {
requestCount++;
const name = req.params.name || 'World';
res.send(`Hello, ${name}! (Request #${requestCount})`);
console.log(`Handled request for: ${name}`);
});
app.listen(8080, () => {
console.log('Server starting on http://localhost:8080');
});typescript
import { Fprintf, Println } from "fmt";
import { HandleFunc, ListenAndServe, Request, ResponseWriter } from "net/http";
let requestCount: int = 0;
function handler(w: ResponseWriter, r: Request): void {
requestCount++;
const name = r.URL.Path.slice(1) || "World";
Fprintf(w, "Hello, %s! (Request #%d)", name, requestCount);
Println(`Handled request for: ${name}`);
}
function main(): void {
HandleFunc("/", handler);
Println("Server starting on http://localhost:8080");
ListenAndServe(":8080", null);
}主要差异:
- 导入: 直接从 Go 标准库导入
- 类型: 使用 Go 类型(
int而非number) - HTTP 处理: 使用 Go 的
net/http包 - 性能: Targo 版本编译为原生 Go,性能更高
下一步
恭喜!你已经创建了第一个 Targo 程序。接下来:
- 核心概念 - 类型系统 - 理解 Targo 的类型系统
- 核心概念 - 集合类型 - 学习 slice、map、chan
- TypeScript 到 Targo - 理解与 TypeScript 的差异
- 示例项目 - 查看更多示例
扩展练习
尝试以下练习来加深理解:
- 添加更多路由: 创建
/api/status端点返回服务器状态 - JSON API: 使用
encoding/json包返回 JSON 响应 - 并发安全: 使用
sync.Mutex保护计数器 - 中间件: 添加日志中间件记录所有请求
提示:使用 targo bind encoding/json sync 绑定需要的包。