用go 语言写一个简易的客户管理系统
时间: 2023-07-08 10:35:18 浏览: 54
好的,以下是一个简单的客户管理系统的 Go 语言代码示例:
```go
package main
import (
"fmt"
)
type Customer struct {
name string
email string
phone string
}
var customers []Customer
func main() {
for {
fmt.Println("Welcome to the Customer Management System!")
fmt.Println("----------------------------------------")
fmt.Println("1. Add Customer")
fmt.Println("2. View Customers")
fmt.Println("3. Exit")
fmt.Println("----------------------------------------")
var choice int
fmt.Print("Enter your choice: ")
fmt.Scanln(&choice)
switch choice {
case 1:
addCustomer()
case 2:
viewCustomers()
case 3:
fmt.Println("Goodbye!")
return
default:
fmt.Println("Invalid choice. Please try again.")
}
}
}
func addCustomer() {
var name, email, phone string
fmt.Print("Enter customer name: ")
fmt.Scanln(&name)
fmt.Print("Enter customer email: ")
fmt.Scanln(&email)
fmt.Print("Enter customer phone: ")
fmt.Scanln(&phone)
customer := Customer{name, email, phone}
customers = append(customers, customer)
fmt.Println("Customer added successfully!")
}
func viewCustomers() {
fmt.Println("Customer List:")
fmt.Println("------------------------")
for _, customer := range customers {
fmt.Printf("Name: %s\nEmail: %s\nPhone: %s\n\n", customer.name, customer.email, customer.phone)
}
}
```
这个程序使用一个 `Customer` 结构体来表示客户信息,然后使用一个切片来存储客户列表。主函数中通过一个无限循环来处理用户输入的选项,并根据选项调用对应的函数来完成相应的操作。`addCustomer()` 函数用来添加新的客户,`viewCustomers()` 函数用来查看所有客户信息。