golang可以编写shell脚本吗
golang是可以编写shell脚本,首先可以通过创建一个阅读器去访问键盘,每当按下回车键时,任何的写入都会被存储到输入变量中;然后根据出入的数据来执行逻辑运算;最后将处理结果使用“fmt.Println()”将数据输出即可。
代码示例:
package main import ( "bufio" "errors" "fmt" "os" "os/exec" "strings") func main() { reader := bufio.NewReader(os.Stdin) for { fmt.Print("> ") // Read the keyboad input. input, err := reader.ReadString('\n') if err != nil { fmt.Fprintln(os.Stderr, err) } // Handle the execution of the input. err = execInput(input) if err != nil { fmt.Fprintln(os.Stderr, err) } } } // ErrNoPath is returned when 'cd' was called without a second argument.var ErrNoPath = errors.New("path required") func execInput(input string) error { // Remove the newline character. input = strings.TrimSuffix(input, "\n") // Split the input separate the command and the arguments. args := strings.Split(input, " ") // Check for built-in commands. switch args[0] { case "cd": // 'cd' to home with empty path not yet supported. if len(args) < 2 { return ErrNoPath } err := os.Chdir(args[1]) if err != nil { return err } // Stop further processing. return nil case "exit": os.Exit(0) } // Prepare the command to execute. cmd := exec.Command(args[0], args[1:]...) // Set the correct output device. cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout // Execute the command and save it's output. err := cmd.Run() if err != nil { return err } return nil}
推荐教程:《Go教程》
以上是golang可以编写shell脚本吗的详细内容。更多信息请关注PHP中文网其他相关文章!
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
刺客信条阴影:贝壳谜语解决方案
3 周前ByDDD
Windows 11 KB5054979中的新功能以及如何解决更新问题
2 周前ByDDD
在哪里可以找到原子中的起重机控制钥匙卡
3 周前ByDDD
<🎜>:死铁路 - 如何完成所有挑战
4 周前ByDDD
Atomfall指南:项目位置,任务指南和技巧
4 周前ByDDD

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)