首先定义商品结构体,包含ID、名称、价格和库存;接着用切片存储商品并实现增删改查函数;然后通过命令行菜单交互,用户可选择功能操作商品;最后程序支持添加、查看、修改、删除商品并退出。1. 结构体设计清晰,2. 功能封装良好,3. 交互简洁易用,4. 便于扩展持久化与错误处理,适合Golang初学者学习基础语法与程序组织。

用Golang实现一个简单的商品管理系统,重点在于结构清晰、接口简洁、便于扩展。本文带你从零开始搭建一个基于命令行的商品管理程序,涵盖商品的增删改查功能,适合初学者理解Golang的基本语法和程序组织方式。
1. 定义商品结构体
首先,我们需要定义一个表示商品的数据结构。每个商品包含ID、名称、价格和库存等基本信息。
type Product struct {
ID int
Name string
Price float64
Quantity int
}
这个结构体是系统的核心数据模型,后续所有操作都围绕它展开。
2. 实现商品管理功能
我们可以创建一个切片来存储商品列表,并封装一组方法来操作这些数据。
立即学习“go语言免费学习笔记(深入)”;
定义一个全局变量或结构体来持有商品数据:
var products []Product var nextID = 1
接下来实现四个基本操作:
- AddProduct:添加新商品,自动生成ID
- GetAllProducts:获取所有商品列表
- UpdateProduct:根据ID更新商品信息
- DeleteProduct:根据ID删除商品
部分功能简介:商品收藏夹功能热门商品最新商品分级价格功能自选风格打印结算页面内部短信箱商品评论增加上一商品,下一商品功能增强商家提示功能友情链接用户在线统计用户来访统计用户来访信息用户积分功能广告设置用户组分类邮件系统后台实现更新用户数据系统图片设置模板管理CSS风格管理申诉内容过滤功能用户注册过滤特征字符IP库管理及来访限制及管理压缩,恢复,备份数据库功能上传文件管理商品类别管理商品添加/修改/
func AddProduct(name string, price float64, quantity int) {
product := Product{
ID: nextID,
Name: name,
Price: price,
Quantity: quantity,
}
products = append(products, product)
nextID++
}
func GetAllProducts() []Product {
return products
}
func UpdateProduct(id int, name string, price float64, quantity int) bool {
for i := range products {
if products[i].ID == id {
products[i].Name = name
products[i].Price = price
products[i].Quantity = quantity
return true
}
}
return false
}
func DeleteProduct(id int) bool {
for i, p := range products {
if p.ID == id {
products = append(products[:i], products[i+1:]...)
return true
}
}
return false
}
3. 构建命令行交互界面
为了让用户能方便地使用系统,我们添加一个简单的菜单驱动的交互逻辑。
func showMenu() {
fmt.Println("\n--- 商品管理系统 ---")
fmt.Println("1. 添加商品")
fmt.Println("2. 查看所有商品")
fmt.Println("3. 修改商品")
fmt.Println("4. 删除商品")
fmt.Println("5. 退出")
fmt.Print("请选择操作: ")
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for {
showMenu()
scanner.Scan()
choice := scanner.Text()
switch choice {
case "1":
fmt.Print("名称: ")
scanner.Scan()
name := scanner.Text()
fmt.Print("价格: ")
scanner.Scan()
price, _ := strconv.ParseFloat(scanner.Text(), 64)
fmt.Print("数量: ")
scanner.Scan()
quantity, _ := strconv.Atoi(scanner.Text())
AddProduct(name, price, quantity)
fmt.Println("✅ 商品添加成功!")
case "2":
ps := GetAllProducts()
fmt.Printf("\n%-5s %-15s %-10s %-8s\n", "ID", "名称", "价格", "库存")
for _, p := range ps {
fmt.Printf("%-5d %-15s %-10.2f %-8d\n", p.ID, p.Name, p.Price, p.Quantity)
}
case "3":
fmt.Print("请输入要修改的商品ID: ")
scanner.Scan()
id, _ := strconv.Atoi(scanner.Text())
fmt.Print("新名称: ")
scanner.Scan()
name := scanner.Text()
fmt.Print("新价格: ")
scanner.Scan()
price, _ := strconv.ParseFloat(scanner.Text(), 64)
fmt.Print("新数量: ")
scanner.Scan()
quantity, _ := strconv.Atoi(scanner.Text())
if UpdateProduct(id, name, price, quantity) {
fmt.Println("✅ 商品更新成功!")
} else {
fmt.Println("❌ 未找到该商品")
}
case "4":
fmt.Print("请输入要删除的商品ID: ")
scanner.Scan()
id, _ := strconv.Atoi(scanner.Text())
if DeleteProduct(id) {
fmt.Println("✅ 商品删除成功!")
} else {
fmt.Println("❌ 未找到该商品")
}
case "5":
fmt.Println("? 退出系统")
return
default:
fmt.Println("⚠️ 无效选择,请重试")
}
}}
4. 运行与优化建议
运行程序后,你可以通过输入数字选择对应功能,完成商品管理的基本流程。
虽然当前版本使用内存存储,但可以进一步改进:
- 将数据持久化到JSON或CSV文件中
- 使用SQLite数据库存储商品信息
- 改为HTTP服务提供REST API接口
- 加入输入校验和错误处理
基本上就这些。这个小项目涵盖了结构体、切片、函数、控制流和用户输入处理,是学习Golang实践的好起点。不复杂但容易忽略细节,比如ID去重和边界检查,实际开发中需要更严谨。









