高软lab2:menu

m0_73571223 2023-03-27 14:32:24

1.搭建go开发环境

2.编写代码

package main

import (
    "fmt"
    "os"
)

var head *DataNode

// some cmds
var cmds []*DataNode = []*DataNode{
    {"help", "this is help cmd!", Help, nil},
    {"version", "menu program v2.0", ShowVersion, nil},
    {"q", "exit menu", Exit, nil},
}

func main() {
    //将cmds插入到链表中
    head = &DataNode{}
    tail := head
    for _, cmd := range cmds {
        tail.next = cmd
        tail = tail.next
    }
    tail.next = nil
    head = head.next

    var cmd string
    for {
        fmt.Print("Input a cmd > ")
        fmt.Scanln(&cmd)
        p := FindCmd(head, cmd)
        if p == nil {
            fmt.Println("This is a wrong cmd!")
            continue
        }
        fmt.Println(p.cmd, " - ", p.desc)
        if p.handler != nil {
            p.handler()
        }
        fmt.Println()
    }
}

func Help() {
    ShowAllCmd(head)
}
func ShowVersion() {
    fmt.Println("menu v2.0")
}
func Exit() {
    os.Exit(0)
}

3.运行结果

img

4.上传到github

img

尾号247

...全文
81 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

571

社区成员

发帖
与我相关
我的任务
社区描述
软件工程教学新范式,强化专项技能训练+基于项目的学习PBL。Git仓库:https://gitee.com/mengning997/se
软件工程 高校
社区管理员
  • 码农孟宁
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧