Skip to content

5 分钟教程

通过这个教程,你会在一个最小项目里完成三件事:

  • 写一个可运行的 Targo 程序
  • 直接使用 Go 标准库
  • 跑通 checkrunbuild 这条基础链路

前置要求

步骤 1:创建项目

bash
npx @targo/create-targo hello-server
cd hello-server

步骤 2:刷新项目状态

bash
targo init

步骤 3:写一个最小 HTTP 服务器

src/main.ts 改成:

typescript
import { Fprintf, Println } 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);
}

export function main(): void {
  HandleFunc("/", handler);
  Println("Server starting on http://localhost:8080");
  ListenAndServe(":8080", null);
}

步骤 4:先检查,再运行

bash
targo check
targo run

打开:

  • http://localhost:8080/
  • http://localhost:8080/Targo

步骤 5:构建 Go 输出

bash
targo build

这时你可以去看 dist/,但它只是构建产物,不是你应该手改的源代码。

通过这个例子你应该学到什么

  • Targo 源码写在 TypeScript 风格语法里
  • 你可以直接使用 Go 标准库
  • 开发节奏通常是 init -> check -> run/build

接下来该看什么

如果你想理解背后的语义差异,继续看:

如果你想确认当前具体支持什么,跳到: