1. 程式人生 > 其它 >Golang 實現登入註冊小程式

Golang 實現登入註冊小程式

目錄

例一

main.go

package main

import (
	"dev_code/user_login/tool"
	"fmt"
)

var (
	n				int
	decision		string
	price			float64
	count			[4]int
	exec			string
	userManagement  = map[string]string{}
	passManagement  = map[string]string{}
	userManagement1 = map[string]string{}
	passManagement1 = map[string]string{}
)

func main() {
	// 註冊
Con:
	userManagement,passManagement = tool.Enroll()
	fmt.Println("您的使用者名稱:",userManagement)
	fmt.Println("您的密碼:",passManagement)

	// 登入
Format:
	userManagement1,passManagement1 = tool.Login()
	for user := range userManagement {
		for pass := range passManagement {
			value,exec := userManagement[user]
			value1,exec := passManagement[pass]
			if exec {
				if value == userManagement1[user] && value1 == passManagement1[pass] {
					fmt.Println(value)
					fmt.Println(value1)
					fmt.Println("歡迎登入!")
					goto END
				} else if value != userManagement1[user] || value != passManagement1[pass] {
					fmt.Println("使用者名稱不存在,請註冊!")
					goto Con
				} else {
					fmt.Println("您輸入的格式錯誤,請重新輸入!")
					n++
					goto Format
				}
			}
		}
	END:
		break
	}
}

tool.go

package tool

import (
	"bufio"
	"fmt"
	"os"
)

// 登入介面
func Login() (map[string]string, map[string]string) {
	user := map[string]string{}
	pass := map[string]string{}
	fmt.Println("歡迎登入商城頁面!")

	fmt.Println("請輸入登入使用者名稱:")
	LoginUser := bufio.NewScanner(os.Stdin)
	if LoginUser.Scan() {
		user["userName"] = LoginUser.Text()
	} else {
		user["userName"] = "您輸入錯誤"
	}

	fmt.Println("請輸入登入密碼:")
	LoginPassword := bufio.NewScanner(os.Stdin)
	if LoginPassword.Scan() {
		pass["PassWord"] = LoginPassword.Text()
	} else {
		pass["PassWord"] = "您輸入錯誤"
	}
	return user,pass
}

// 註冊
func Enroll() (map[string]string, map[string]string) {
	user := map[string]string{}
	pass := map[string]string{}
	fmt.Println("歡迎登入商城頁面!")
	fmt.Println("請輸入註冊使用者名稱:")
	LoginUser := bufio.NewScanner(os.Stdin)
	if LoginUser.Scan() {
		user["userName"] = LoginUser.Text()
	}
	for i:=0;i<3;i++ {
		fmt.Println("請輸入註冊密碼:")
		LoginPassword := bufio.NewScanner(os.Stdin)
		if LoginPassword.Scan() {
			pass["PassWord"] = LoginPassword.Text()
		}
		fmt.Println("請再次輸入註冊密碼:")
		LoginRePassword := bufio.NewScanner(os.Stdin)
		if LoginRePassword.Scan() {
			pass["PassWord"] = LoginRePassword.Text()
		}
		if LoginPassword.Text() == LoginRePassword.Text() {
			fmt.Println("密碼輸入正確")
			break
		} else {
			fmt.Println("兩次密碼輸入錯誤,請重新輸入!")
			continue
		}
	}
	return user,pass
}