社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
访问 Go 官网 https://golang.org,下载对应平台安装包,MacOs 下载 pkg 包一键安装,自动将 go 添加到了环境变量,直接可以使用 go 命令。
// helloworld.go
package main
import "fmt"
func main() {
fmt.Println("hello, world!")
}
两种方法:
go run helloworld.go
go build helloworld.go
./helloworld
Go IDE 选择 JetBrains 公司的 Goland:https://www.jetbrains.com/go
Goland 是商业软件,不过可以免费激活:https://gitee.com/pengzhile/jetbrains-agent
主题配置:Material Theam UI + Material Oceanic 配色方案
Material Theam UI 插件安装:GoLand–>Preferences–>Plugins–>Install
Material Oceanic 配色方案:GoLand–>Preferences–>Editor–>Color Scheme–>选择 Material Oceanic
最终效果:
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"sorter/sorter/algorithms/bubblesort"
"strconv"
"time"
)
var infile = flag.String("i", "infile", "File contains values for sorting")
var outfile = flag.String("o", "outfile", "File to receive sorted values")
var algorithm = flag.String("a", "qsort", "Sort algorithm")
func readValues(infile string) (values []int, err error) {
file, err := os.Open(infile)
if err != nil {
fmt.Println("Failed to open the input file", infile)
}
defer file.Close()
br := bufio.NewReader(file)
values = make([]int, 0)
for {
line, isPrefix, err1 := br.ReadLine()
if err1 != nil {
if err1 != io.EOF {
err = err1
}
break
}
if isPrefix {
fmt.Println("A too long line, seems unexpected.")
return
}
str := string(line)
value, err1 := strconv.Atoi(str)
if err1 != nil {
err = err1
return
}
values = append(values, value)
}
return
}
func writeValues(values []int, outfile string) error {
file, err := os.Create(outfile)
if err != nil {
fmt.Println("Failed to create the output file ", outfile)
return err
}
defer file.Close()
for _, value := range values {
str := strconv.Itoa(value)
file.WriteString(str + "n")
}
return nil
}
func main() {
flag.Parse()
if infile != nil {
fmt.Println("infile=", *infile, "outfile=", *outfile, "algorithm=", *algorithm)
}
values, err := readValues(*infile)
if err == nil {
t1 := time.Now()
switch *algorithm {
case "bubblesort":
bubblesort.BubbleSort(values)
default:
fmt.Println("Sorting algorithm", *algorithm, "is either unknown or unsupported.")
}
t2 := time.Now()
fmt.Println("The sorting process costs", t2.Sub(t1), "to complete.")
writeValues(values, *outfile)
} else {
fmt.Println(err)
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!