*Python/Java/Go** 准备的详细指南,涵盖环境搭建、基础语法、实战项目(含代码)及避坑指南
一、Python 篇**
目标:开发一个简单的“天气查询工具”
1. 环境搭建
- 步骤:
- 访问官网下载 Python(推荐 3.9+):
https://www.python.org/downloads/ - 安装时勾选“Add Python to PATH”,自动配置环境变量。
- 验证安装:打开终端/命令提示符,输入
python --version,显示版本号即成功。
- 访问官网下载 Python(推荐 3.9+):
- 避坑指南:
- 避免同时安装多个 Python 版本,可能引起冲突。
- 若报错“不是内部命令”,需手动配置环境变量(将 Python 安装目录添加到
PATH)。
2. 开发工具推荐
- VS Code(轻量且插件丰富)或PyCharm Community(功能强大)。
- 安装插件:Python、Linter(代码检查)。
3. 基础语法快速入门
# Hello Worldprint("Hello, Python!")# 变量与数据类型name="Alice"age=25is_student=True# 控制流ifage>18:print("Adult")else:print("Teenager")# 循环foriinrange(5):print(i)# 函数defgreet(name):returnf"Hi,{name}!"print(greet("Bob"))4. 实战项目:天气查询工具
- 需求:通过 API 查询城市天气。
- 步骤:
- 安装
requests库:pip install requests - 编写代码:
- 安装
importrequestsdefget_weather(city):api_key="YOUR_API_KEY"# 替换为真实 Key(注册天气 API 获取)url=f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"response=requests.get(url)data=response.json()returnf"{city}天气:{data['main']['temp']}℃(当前温度),{data['weather'][0]['description']}"print(get_weather("Beijing"))- 运行:在终端执行
python weather.py。
避坑指南:
- API Key 需保密,避免硬编码(可存储在环境变量中)。
- 处理网络请求异常(如超时、404)。
- 使用
try-except捕获错误。
二、Java 篇
目标:开发一个命令行“学生管理系统”
1. 环境搭建
- 步骤:
- 下载并安装 JDK(推荐 11+):
https://www.oracle.com/java/technologies/downloads/ - 配置环境变量:
JAVA_HOME:JDK 安装目录PATH:添加%JAVA_HOME%\bin
- 验证:
java -version和javac -version。
- 下载并安装 JDK(推荐 11+):
- 避坑指南:
- 确保
JAVA_HOME指向正确路径(避免空格或特殊字符)。 - 若报错“找不到命令”,检查 Path 配置是否包含 Java 的 bin 目录。
- 确保
2. 开发工具
- IntelliJ IDEA Community(推荐)或 Eclipse。
3. 基础语法快速入门
// Hello WorldpublicclassHelloWorld{publicstaticvoidmain(String[]args){System.out.println("Hello, Java!");}}// 类和对象classStudent{Stringname;intage;publicvoidstudy(){System.out.println(name+" is studying.");}}publicclassApp{publicstaticvoidmain(String[]args){Students=newStudent();s.name="Alice";s.study();}}4. 实战项目:学生管理系统
- 需求:添加、查询、删除学生信息(基于控制台交互)。
- 步骤:
- 创建
Student类(含姓名、年龄、成绩)。 - 主逻辑:使用
Scanner接收用户输入,管理学生列表(用ArrayList)。 - 代码示例(简化版):
- 创建
importjava.util.*;publicclassStudentManager{privateList<Student>students=newArrayList<>();publicvoidaddStudent(){Scannersc=newScanner(System.in);System.out.print("Enter name: ");Stringname=sc.nextLine();// ... 其他信息students.add(newStudent(name));}// ... 查询、删除方法}publicclassMain{publicstaticvoidmain(String[]args){StudentManagersm=newStudentManager();while(true){// 显示菜单,调用对应方法}}}避坑指南:
- 注意
null指针异常(使用前检查对象是否初始化)。 - 使用
try-with-resources关闭Scanner。 - 避免硬编码数据(可引入文件或数据库存储)。
三、Go 篇
目标:开发一个“RESTful API 服务”(返回 JSON 数据)
1. 环境搭建
- 步骤:
- 下载 Go:
https://golang.org/dl/ - 配置环境变量:
GOROOT:Go 安装目录GOPATH:工作空间(如D:\go)PATH:添加%GOROOT%\bin和%GOPATH%\bin
- 验证:
go version。
- 下载 Go:
- 避坑指南:
- 确保
GOPATH设置正确,避免模块管理问题。 - Windows 需启用开发者模式(允许执行脚本)。
- 确保
2. 开发工具
- VS Code + Go 扩展或Goland。
3. 基础语法快速入门
// Hello Worldpackagemainimport"fmt"funcmain(){fmt.Println("Hello, Go!")}// 结构体与方法typeUserstruct{NamestringAgeint}func(u User)SayHi(){fmt.Printf("%s says hello!\n",u.Name)}// 接口与多态typeGreeterinterface{Greet()}typeBotstruct{}func(b Bot)Greet(){fmt.Println("Hi from Bot!")}4. 实战项目:RESTful API(用户服务)
- 需求:提供
/users接口,返回用户列表(JSON)。 - 步骤:
- 安装依赖:
go get -u github.com/gin-gonic/gin - 编写代码:
- 安装依赖:
packagemainimport("net/http""github.com/gin-gonic/gin")typeUserstruct{IDint`json:"id"`Namestring`json:"name"`}varusers=[]User{{1,"Alice"},{2,"Bob"},}funcmain(){r:=gin.Default()r.GET("/users",func(c*gin.Context){c.JSON(http.StatusOK,users)})r.Run(":8080")}- 运行:
go run main.go,访问http://localhost:8080/users。
避坑指南:
- 端口冲突(使用
lsof或netstat检查并释放)。 - 跨域问题(配置 Gin 中间件)。
- 依赖管理:使用
go mod初始化模块(go mod init myapp)。
最后提醒:编程初期会遇到大量报错,善用搜索引擎+错误信息定位问题。保持耐心,逐步积累!
