跳到主要内容

ChatGPT 开发者指南:完整编程教程

7 天掌握 ChatGPT 编程 - 从基础到高级技巧,附带真实案例。

🎯 你将学到什么

完成本指南后,你将能够:

  • ✅ 编写有效的代码生成提示词
  • ✅ 使用 ChatGPT 将调试速度提升 3 倍
  • ✅ 在几小时内学会新框架,而不是几天
  • ✅ 自信地重构遗留代码
  • ✅ 生成全面的测试
  • ✅ 优化你的开发工作流程

时间投入: 每天 30 分钟 × 7 天 技能水平: 从初级到高级 费用: 免费(ChatGPT Free)或每月 $20(GPT-4 的 Plus 版本)


🎬 快速演示(30 秒)

视频即将推出

📹 演示视频制作中 - 预计发布时间:2025-11-17 周

视频将演示:

  • 使用 ChatGPT 实时生成代码
  • 借助 AI 辅助调试 React 组件
  • 逐步重构遗留代码
  • 测试生成工作流程

你将在 30 秒内看到:

  • 如何构建提示词以获得更好的代码质量
  • 实时调试会话:查找和修复 bug
  • AI 辅助重构的前后对比
  • 生产力提升:手动编码 vs. ChatGPT 辅助

视频发布时将提供完整文字记录


第 1 天:基础知识

了解 ChatGPT 的优势和劣势

ChatGPT 擅长的领域:

  • ✅ 解释代码概念
  • ✅ 生成样板代码
  • ✅ 调试逻辑错误
  • ✅ 编写测试
  • ✅ 在不同语言间转换代码

ChatGPT 不擅长的领域:

  • ❌ 非常新的库(2023 年后)
  • ❌ 公司特定代码
  • ❌ 复杂的系统设计
  • ❌ 安全关键代码(需要审查)

你的第一个提示词

❌ 不好的提示词:

write a login function

✅ 好的提示词:

Create a secure login function in Python using Flask and SQLAlchemy.

Requirements:
- Hash passwords with bcrypt
- Return JWT token on success
- Handle errors (user not found, wrong password)
- Include input validation
- Add docstring with examples

Use type hints and follow PEP 8.

ChatGPT 输出:

from flask import jsonify, request
from werkzeug.security import check_password_hash
from models import User
import jwt
import datetime
from typing import Tuple, Dict
import re

def validate_email(email: str) -> bool:
"""Validate email format using regex."""
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
return re.match(pattern, email) is not None

def login_user(email: str, password: str) -> Tuple[Dict, int]:
"""
Authenticate user and return JWT token.

Args:
email: User's email address
password: Plain text password

Returns:
Tuple of (response_dict, http_status_code)

Example:
>>> login_user('user@example.com', 'password123')
({'token': 'eyJ0eXAiOiJKV1...'}, 200)

Raises:
ValueError: If email format is invalid
"""

# Input validation
if not email or not password:
return {'error': 'Email and password are required'}, 400

if not validate_email(email):
return {'error': 'Invalid email format'}, 400

# Find user
user = User.query.filter_by(email=email).first()

if not user:
return {'error': 'Invalid credentials'}, 401

# Verify password
if not check_password_hash(user.password_hash, password):
return {'error': 'Invalid credentials'}, 401

# Generate JWT token
token = jwt.encode({
'user_id': user.id,
'email': user.email,
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=24)
}, app.config['SECRET_KEY'], algorithm='HS256')

return {
'token': token,
'user': {
'id': user.id,
'email': user.email,
'name': user.name
}
}, 200

关键经验: 具体的提示词 → 更好的代码

实践练习

使用好的提示词模式为你的项目生成一个函数:

  1. 指定语言和框架
  2. 列出需求
  3. 请求错误处理
  4. 要求提供文档
  5. 指定代码风格

第 2 天:使用 ChatGPT 调试

调试模式

模板:

I'm getting this error:
[粘贴错误信息]

Here's my code:
[粘贴相关代码]

Context:
- Language/Framework: [例如,React 18 + TypeScript]
- What I'm trying to do: [简要说明]
- What I've tried: [列出尝试过的方法]

Please explain what's wrong and how to fix it.

真实案例:React 无限渲染

你的代码:

function UserProfile({ userId }) {
const [user, setUser] = useState(null);

useEffect(() => {
fetch(`/api/users/${userId}`)
.then(res => res.json())
.then(setUser);
}); // Bug: missing dependency array

return <div>{user?.name}</div>;
}

错误:

Warning: Maximum update depth exceeded. This can happen when a component
calls setState inside useEffect without a dependency array.

向 ChatGPT 提问:

I'm getting "Maximum update depth exceeded" error in React.

Code:
[粘贴上面的代码]

Context:
- React 18 + TypeScript
- Trying to fetch user data on component mount
- Error appears immediately on page load

Please explain and fix.

ChatGPT 回复:

The problem is the missing dependency array in useEffect.

Current code:
useEffect(() => {
fetch(`/api/users/${userId}`)
.then(res => res.json())
.then(setUser);
}); // Runs after EVERY render

What happens:
1. Component renders
2. useEffect runs, fetches data
3. setUser() causes re-render
4. useEffect runs again (because no dependency array)
5. Infinite loop!

Fix:
useEffect(() => {
fetch(`/api/users/${userId}`)
.then(res => res.json())
.then(setUser);
}, [userId]); // Only run when userId changes

Complete improved version:
useEffect(() => {
let cancelled = false;

const fetchUser = async () => {
try {
const res = await fetch(`/api/users/${userId}`);
if (!res.ok) throw new Error('Failed to fetch user');

const data = await res.json();
if (!cancelled) setUser(data);
} catch (error) {
console.error('Error fetching user:', error);
// Handle error state
}
};

fetchUser();

return () => {
cancelled = true; // Cleanup to prevent state update on unmounted component
};
}, [userId]);

调试能力: ChatGPT 解释原因(不仅仅是如何修复)

实践练习

使用上面的模板调试代码库中的实际问题。


第 3 天:学习新技术

学习模式

不要花几个小时阅读文档,而是将 ChatGPT 用作导师:

阶段 1:概述(5 分钟)

Explain [Technology] in simple terms for an experienced developer.

Include:
- What problem it solves
- Key concepts (5-7 most important)
- When to use it vs alternatives
- 30-second example

Example: React developer learning Vue 3

阶段 2:Hello World(10 分钟)

Create a simple [App Type] using [Technology].

Requirements:
- [Feature 1]
- [Feature 2]
- [Feature 3]
- Include comments explaining key parts
- Production-ready code quality

Example: Todo app with Vue 3 Composition API

阶段 3:真实世界模式(15 分钟)

Show me best practices for [Specific Task] in [Technology].

Include:
- Modern approach (2024)
- Error handling
- Performance considerations
- Common pitfalls to avoid
- Code example

Example: State management in Vue 3

真实案例:学习 FastAPI

提示词 1:

Explain FastAPI for an Express.js developer.

Include:
- Key differences from Express
- Main advantages
- When to choose FastAPI
- 30-second example

ChatGPT 提供清晰的对比:

# FastAPI (Python) vs Express (JavaScript) - Key Differences

1. Type Safety: FastAPI uses Python type hints for automatic validation
2. Auto Documentation: Built-in Swagger UI (no extra setup)
3. Async: Native async/await support
4. Performance: Fast as Node.js (due to Starlette/Uvicorn)

When to use FastAPI:
✅ Python ecosystem (ML, data science)
✅ Need automatic API docs
✅ Strong typing important
❌ Node.js ecosystem required
❌ Team only knows JavaScript

# 30-second example
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
name: str
email: str

@app.post("/users")
async def create_user(user: User):
# Type validation automatic!
# API docs automatic!
return {"id": 123, **user.dict()}

# Run: uvicorn main:app --reload
# Docs: http://localhost:8000/docs (auto-generated!)

提示词 2:

Create a REST API for blog posts using FastAPI.

Requirements:
- CRUD operations (create, read, update, delete)
- SQLite database with SQLAlchemy
- Pydantic models for validation
- Error handling
- Include all necessary imports

结果: 2 分钟内完成完整可用的 API

实践练习

在 30 分钟内学习一个新的框架/库:

  1. 获取概述提示词
  2. 构建 hello world 提示词
  3. 实现真实功能提示词

第 4 天:代码重构

重构模式

Refactor this code to improve [specific aspect]:

[粘贴代码]

Requirements:
- [Improvement 1]
- [Improvement 2]
- Keep functionality identical
- Add comments explaining changes

Context:
- [Framework/language details]
- [Performance requirements if any]

真实案例:清理混乱的 React 组件

原始代码:

// 300-line component with everything mixed together
function Dashboard() {
const [users, setUsers] = useState([]);
const [products, setProducts] = useState([]);
const [orders, setOrders] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
// ... 10 more state variables

useEffect(() => {
// 100 lines of data fetching logic
fetch('/api/users').then(...)
fetch('/api/products').then(...)
fetch('/api/orders').then(...)
// ... more fetching
}, []);

// 50 lines of event handlers

return (
<div>
{/* 150 lines of JSX */}
</div>
);
}

提示词:

Refactor this React component using best practices:

[粘贴代码]

Requirements:
- Extract custom hooks for data fetching
- Separate into smaller components
- Use React Query for caching
- Add proper TypeScript types
- Improve loading/error states

ChatGPT 输出:

// 1. Custom hook for data fetching
import { useQuery } from '@tanstack/react-query';

function useDashboardData() {
const users = useQuery({
queryKey: ['users'],
queryFn: () => fetch('/api/users').then(r => r.json())
});

const products = useQuery({
queryKey: ['products'],
queryFn: () => fetch('/api/products').then(r => r.json())
});

const orders = useQuery({
queryKey: ['orders'],
queryFn: () => fetch('/api/orders').then(r => r.json())
});

return {
users: users.data,
products: products.data,
orders: orders.data,
isLoading: users.isLoading || products.isLoading || orders.isLoading,
error: users.error || products.error || orders.error
};
}

// 2. Main component (now clean)
export function Dashboard() {
const { users, products, orders, isLoading, error } = useDashboardData();

if (isLoading) return <LoadingSpinner />;
if (error) return <ErrorMessage error={error} />;

return (
<DashboardLayout>
<UsersSection users={users} />
<ProductsSection products={products} />
<OrdersSection orders={orders} />
</DashboardLayout>
);
}

// 3. Separated components
function UsersSection({ users }: { users: User[] }) {
return (
<section>
<h2>Users</h2>
<UserList users={users} />
</section>
);
}

// ... more components

重构前: 300 行,难以测试,难以阅读 重构后: 50 行主组件,5 个聚焦的子组件,易于测试

实践练习

在你的代码库中找一个混乱的文件,用 ChatGPT 重构它。


第 5 天:测试生成

测试模式

Generate comprehensive tests for this code:

[粘贴代码]

Requirements:
- Framework: [Jest/Pytest/etc]
- Cover happy path and edge cases
- Include setup/teardown if needed
- Mock external dependencies
- Aim for >90% coverage

真实案例:测试 API 端点

要测试的代码:

@app.route('/api/discounts', methods=['POST'])
def calculate_discount():
data = request.get_json()

price = data.get('price')
user_tier = data.get('user_tier')
promo_code = data.get('promo_code')

if not price or price <= 0:
return jsonify({'error': 'Invalid price'}), 400

discount = 0

# Tier discounts
if user_tier == 'bronze': discount += 5
elif user_tier == 'silver': discount += 10
elif user_tier == 'gold': discount += 15

# Promo code
if promo_code == 'SAVE20':
discount += 20

final_price = price * (1 - discount / 100)

return jsonify({
'original_price': price,
'discount_percent': discount,
'final_price': final_price
})

提示词:

Generate pytest tests for this Flask endpoint:

[粘贴代码]

Requirements:
- Test all user tiers
- Test with/without promo code
- Test edge cases (negative price, invalid tier, etc.)
- Test promo code stacking with tiers
- Use pytest fixtures for app setup

ChatGPT 输出:

import pytest
from flask import Flask
from decimal import Decimal

@pytest.fixture
def app():
"""Create Flask app for testing"""
app = create_app('testing')
return app

@pytest.fixture
def client(app):
"""Create test client"""
return app.test_client()

class TestDiscountCalculation:
"""Test discount calculation endpoint"""

def test_no_tier_no_promo(self, client):
"""Test base case: no discount"""
response = client.post('/api/discounts', json={
'price': 100,
'user_tier': None,
'promo_code': None
})

assert response.status_code == 200
data = response.get_json()
assert data['original_price'] == 100
assert data['discount_percent'] == 0
assert data['final_price'] == 100

@pytest.mark.parametrize('tier,expected_discount', [
('bronze', 5),
('silver', 10),
('gold', 15),
])
def test_tier_discounts(self, client, tier, expected_discount):
"""Test each tier discount level"""
response = client.post('/api/discounts', json={
'price': 100,
'user_tier': tier,
'promo_code': None
})

data = response.get_json()
assert data['discount_percent'] == expected_discount
assert data['final_price'] == 100 - expected_discount

def test_promo_code_alone(self, client):
"""Test promo code without tier"""
response = client.post('/api/discounts', json={
'price': 100,
'user_tier': None,
'promo_code': 'SAVE20'
})

data = response.get_json()
assert data['discount_percent'] == 20
assert data['final_price'] == 80

def test_tier_plus_promo_stacking(self, client):
"""Test that tier and promo discounts stack"""
response = client.post('/api/discounts', json={
'price': 100,
'user_tier': 'gold', # 15%
'promo_code': 'SAVE20' # 20%
})

data = response.get_json()
assert data['discount_percent'] == 35 # 15 + 20
assert data['final_price'] == 65

def test_invalid_price_zero(self, client):
"""Test error handling for zero price"""
response = client.post('/api/discounts', json={
'price': 0,
'user_tier': 'gold'
})

assert response.status_code == 400
assert 'error' in response.get_json()

def test_invalid_price_negative(self, client):
"""Test error handling for negative price"""
response = client.post('/api/discounts', json={
'price': -50,
'user_tier': 'gold'
})

assert response.status_code == 400

def test_missing_price(self, client):
"""Test error when price not provided"""
response = client.post('/api/discounts', json={
'user_tier': 'gold'
})

assert response.status_code == 400

def test_invalid_promo_code(self, client):
"""Test that invalid promo codes are ignored"""
response = client.post('/api/discounts', json={
'price': 100,
'user_tier': 'silver',
'promo_code': 'INVALID'
})

data = response.get_json()
# Should only apply tier discount (10%)
assert data['discount_percent'] == 10

def test_large_price(self, client):
"""Test with large price values"""
response = client.post('/api/discounts', json={
'price': 999999.99,
'user_tier': 'gold',
'promo_code': 'SAVE20'
})

assert response.status_code == 200
data = response.get_json()
# 35% off 999999.99
expected = 999999.99 * 0.65
assert abs(data['final_price'] - expected) < 0.01

覆盖率: 95%+ 包含所有边缘情况

实践练习

为你最近编写的函数生成测试。


第 6 天:高级提示词技巧

技巧 1:思维链

要求 ChatGPT 解释其推理过程:

[你的编码问题]

Please think step-by-step:
1. Analyze the requirements
2. Consider edge cases
3. Choose the best approach
4. Implement the solution
5. Explain trade-offs

示例:

Design a rate limiting system for an API.

Think step-by-step about:
1. Different rate limiting algorithms
2. Where to store rate limit data
3. Edge cases (distributed systems, clock skew)
4. Implementation approach
5. Trade-offs of each decision

技巧 2:迭代改进

[初始请求]

# 看到输出后:
"Great! Now improve it by [specific change]"

# 然后:
"Add [feature]"

# 然后:
"Optimize for [goal]"

技巧 3:角色扮演提示

Act as a senior [role] with 10 years experience.

Review this code and provide:
- What a junior would miss
- Best practices specific to [domain]
- Production readiness checklist

Code:
[粘贴代码]

示例:

Act as a senior DevOps engineer.

Review this Dockerfile and provide:
- Security issues
- Performance optimizations
- Production readiness checklist

Dockerfile:
FROM python:latest
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD python app.py

ChatGPT(作为 DevOps 专家):

# Issues found:

1. SECURITY: Using 'latest' tag (unpredictable)
2. SECURITY: Running as root (default)
3. PERFORMANCE: Installing all deps every build (no caching)
4. PERFORMANCE: Large image size (python:latest ~900MB)
5. RELIABILITY: No health check
6. DEBUGGING: No logging configuration

# Improved Dockerfile:

FROM python:3.11-slim as builder # ✅ Specific version, smaller base

# Create non-root user
RUN useradd -m -u 1000 appuser

# Install dependencies (cached layer)
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy app code
COPY --chown=appuser:appuser . .

# Switch to non-root user
USER appuser

# Health check
HEALTHCHECK --interval=30s --timeout=3s \
CMD python -c "import requests; requests.get('http://localhost:8000/health')"

# Expose port
EXPOSE 8000

# Run app
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:app"]

# Production checklist:
# ✅ Specific base image version
# ✅ Non-root user
# ✅ Dependency caching
# ✅ Small image size (~150MB vs 900MB)
# ✅ Health check
# ✅ Production WSGI server (gunicorn)
# ✅ Proper logging (gunicorn logs to stdout)

第 7 天:构建你的工作流程

创建你的提示词库

保存有效的提示词以供重复使用:

# prompts.md

## Code Review
Act as a senior developer. Review this [language] code for:
1. Bugs and logic errors
2. Performance issues
3. Security vulnerabilities
4. Code style and readability
5. Missing edge cases

Provide specific line numbers and fixes.

---

## Documentation
Generate comprehensive documentation for this code:
- Purpose and overview
- Parameters with types
- Return values
- Usage examples
- Edge cases and error handling

Format: [JSDoc/docstring/etc]

---

## Refactoring
Refactor this code following [framework] best practices:
- Improve readability
- Reduce complexity
- Optimize performance
- Add proper types
- Follow [style guide]

Explain each major change.

键盘快捷键 / IDE 集成

VS Code 扩展: ChatGPT - Genie AI

  • 选择代码 → 右键 → "Ask ChatGPT"
  • 键盘快捷键:Ctrl+Shift+G

命令行:

# Create alias
alias askgpt='function _ask(){ curl -X POST https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d "{\"model\":\"gpt-4\",\"messages\":[{\"role\":\"user\",\"content\":\"$1\"}]}" | jq -r ".choices[0].message.content"; }; _ask'

# Usage
askgpt "Explain this regex: ^[\w\.-]+@[\w\.-]+\.\w+$"

日常工作流程集成

早上:

  • 使用 ChatGPT 查看隔夜错误
  • 计划当天任务(架构问题)

编码期间:

  • 生成样板代码
  • 即时调试错误
  • 解释不熟悉的代码

提交前:

  • 生成测试
  • 编写提交信息
  • 审查更改

代码审查:

  • 在人工审查前进行 AI 审查
  • 解释复杂的更改以用于 PR 描述

💡 专业提示

1. 始终验证 AI 代码

# 不要盲目信任!
# ❌ 错误:不阅读就复制粘贴
# ✅ 正确:阅读、理解、测试

ai_generated_code = get_from_chatgpt()

# 阅读它
understand(ai_generated_code)

# 测试它
run_tests(ai_generated_code)

# 审查安全性
check_security(ai_generated_code)

# 然后使用它

2. 使用 ChatGPT 学习,而不仅仅是获取答案

# ❌ 错误:"Fix my code"
# ✅ 正确:"Explain why this doesn't work and teach me the concept"

3. 结合文档使用

# 最佳工作流程:
1. 阅读官方文档(15 分钟)
2. 向 ChatGPT 寻求澄清(5 分钟)
3. 尝试实现(20 分钟)
4. 如需要,使用 ChatGPT 调试(10 分钟)

总计:50 分钟学会新概念

4. 跟踪成本(GPT-4)

# 如果使用 API
import openai

def count_tokens(text):
# 近似:1 个 token ≈ 4 个字符
return len(text) // 4

prompt = "Your prompt here"
tokens = count_tokens(prompt)
cost = tokens * $0.03 / 1000 # GPT-4 定价

print(f"Estimated cost: ${cost:.4f}")

⚠️ 常见陷阱

1. 幻觉函数

ChatGPT 可能会发明不存在的函数:

# ChatGPT 可能会建议:
result = pandas.magic_function(df) # ❌ 不存在!

# 始终在文档中验证

2. 过时的信息

训练数据截止日期(GPT-4 为 2024 年 1 月):

# ❌ ChatGPT 不知道:
- 2024 年后发布的库
- 最新的 API 更改
- 最近的安全漏洞

# ✅ 解决方案:在提示词中提供文档

3. 过度工程化

ChatGPT 倾向于建议复杂的解决方案:

# 你问:"Store user preferences"

# ChatGPT 建议:Redis + RabbitMQ + Kafka + Microservices

# 现实:JSON 文件对你的用例来说已经足够了

# 始终质疑:"这种复杂性有必要吗?"

📊 衡量你的进步

7 天后,你应该能够:

任务之前之后改进
调试时间30 分钟/bug10 分钟/bug快 3 倍
学习新库4 小时1 小时快 4 倍
编写测试2x 代码时间0.5x 代码时间快 4 倍
代码审查45 分钟15 分钟快 3 倍

投资回报率计算:

  • 节省时间:每天约 2 小时
  • 成本:免费或每月 $20
  • 价值:无价(学得更快,交付更快)

🚀 下一步

第 2 周: 高级技巧

  • 将 ChatGPT 与其他工具结合使用(Copilot、Cursor)
  • 为你的技术栈构建自定义 GPT
  • 自动化重复任务

第 2 个月: 团队采用

  • 与团队分享提示词库
  • 建立最佳实践
  • 衡量团队生产力提升

第 1 季度: 掌握 AI 辅助开发

  • 在 AI 辅助下构建完整功能
  • 使用 AI 教授初级开发人员
  • 更快地为开源做出贡献

🔗 资源


最后更新: 2025-11-09 | 难度: 初级到高级 | 时间: 7 天

想探索其他 AI 编码工具?查看我们的 完整对比指南