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

用Golang实现一个简单的商品管理系统,重点在于结构清晰、接口简洁、便于扩展。本文带你从零开始搭建一个基于命令行的商品管理程序,涵盖商品的增删改查功能,适合初学者理解Golang的基本语法和程序组织方式。
首先,我们需要定义一个表示商品的数据结构。每个商品包含ID、名称、价格和库存等基本信息。
type Product struct {
ID int
Name string
Price float64
Quantity int
}
这个结构体是系统的核心数据模型,后续所有操作都围绕它展开。
我们可以创建一个切片来存储商品列表,并封装一组方法来操作这些数据。
立即学习“go语言免费学习笔记(深入)”;
定义一个全局变量或结构体来持有商品数据:
var products []Product var nextID = 1
接下来实现四个基本操作:
Modoer 是一款以本地分享,多功能的点评网站管理系统。采用 PHP+MYSQL 开发设计,开放全部源代码。因具有非凡的访问速度和卓越的负载能力而深受国内外朋友的喜爱,不局限于商铺类点评,真正实现了多类型的点评,可以让您的网站点评任何事与物,同时增加产品模块,也更好的网站产品在网站上展示。Modoer点评系统 2.5 Build 20110710更新列表1.同步 旗舰版系统框架2.增加 限制图片
0
func AddProduct(name string, price float64, quantity int) {
product := Product{
ID: nextID,
Name: name,
Price: price,
Quantity: quantity,
}
products = append(products, product)
nextID++
}
<p>func GetAllProducts() []Product {
return products
}</p><p>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
}</p><p>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
}</p>为了让用户能方便地使用系统,我们添加一个简单的菜单驱动的交互逻辑。
func showMenu() {
fmt.Println("\n--- 商品管理系统 ---")
fmt.Println("1. 添加商品")
fmt.Println("2. 查看所有商品")
fmt.Println("3. 修改商品")
fmt.Println("4. 删除商品")
fmt.Println("5. 退出")
fmt.Print("请选择操作: ")
}
<p>func main() {
scanner := bufio.NewScanner(os.Stdin)
for {
showMenu()
scanner.Scan()
choice := scanner.Text()</p><pre class='brush:php;toolbar:false;'> 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("⚠️ 无效选择,请重试")
}
}}
运行程序后,你可以通过输入数字选择对应功能,完成商品管理的基本流程。
虽然当前版本使用内存存储,但可以进一步改进:
基本上就这些。这个小项目涵盖了结构体、切片、函数、控制流和用户输入处理,是学习Golang实践的好起点。不复杂但容易忽略细节,比如ID去重和边界检查,实际开发中需要更严谨。
以上就是Golang如何实现简单的商品管理系统_Golang 商品管理系统实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号