Ai
1 Star 0 Fork 0

叶明志/golang练习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.90 KB
一键复制 编辑 原始数据 按行查看 历史
yemingzhi 提交于 2019-07-07 17:16 +08:00 . 文件读写 缓冲区的使用
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
)
func PrintToTerminal() { //向目录下写文字
file, err := os.OpenFile("/home/hu/go/src/hello.txt", os.O_CREATE|os.O_WRONLY, 0664)
defer file.Close()
if err != nil {
fmt.Println("open file err,:", err)
return
}
fmt.Fprintf(file, "do balance err\n")
}
type Student struct {
Name string
Age int
Score float32
}
func (p *Student) String() string {
var score string = fmt.Sprintf("%f", p.Score)
return p.Name + " " + strconv.Itoa(p.Age) + " " + score
}
func fmtFmt() {
var str = "stu01 18 91.55"
var stu Student
fmt.Sscanf(str, "%s %d %f", &stu.Name, &stu.Age, &stu.Score)
fmt.Println(&stu)
}
var inputReader *bufio.Reader
var input string
var err error
func bufioBufio() { //read file by terminal
inputReader := bufio.NewReader(os.Stdin)
fmt.Println("Please enter some input:")
input, err := inputReader.ReadString('\n') //单引号是比特,双引号是字符串
if err != nil {
fmt.Printf("read string failed:%s\n", input)
}
fmt.Printf("read file success,return :%s", input)
}
func readFile() {
file, err := os.Open("/home/hu/go/src/hello.txt")
defer file.Close()
if err != nil {
fmt.Println("read file err\n", err)
return
}
inputReader := bufio.NewReader(file)
// fmt.Println("Please enter some input:")
input, err := inputReader.ReadString('\n') //单引号是比特,双引号是字符串
if err != nil {
fmt.Printf("read string failed:%s\n", input)
}
fmt.Printf("read file success,return :%s", input)
}
type CharCount struct {
ChCount int
NUmCount int
SpaceCount int
OtherCount int
}
func countInput() {
file, err := os.Open("/home/hu/go/src/hello.txt")
defer file.Close()
if err != nil {
fmt.Printf("read file err:%s\n", err)
return
}
var count CharCount
reader := bufio.NewReader(file)
for {
str, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("read string failed,err:%s\n", err)
}
runeArr := []rune(str)
for _, v := range runeArr {
switch {
case v >= 'a' && v <= 'z':
fallthrough
case v >= 'A' && v <= 'Z':
count.ChCount++
case v >= '0' && v <= '9':
count.NUmCount++
case v == ' ':
count.SpaceCount++
default:
count.OtherCount++
}
}
}
fmt.Printf("charCount is %d\n", count.ChCount)
fmt.Printf("NumCount is %d\n", count.NUmCount)
fmt.Printf("SpaceCount is %d\n", count.SpaceCount)
fmt.Printf("OtherCount is %d\n", count.OtherCount)
}
func testioutil() {
inputFile := "/home/hu/go/src/hello.txt"
outputFile := "/home/hu/go/src/world.txt"
buf, err := ioutil.ReadFile(inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "File Error:%s\n", err)
return
}
fmt.Printf("%s\n", string(buf))
err = ioutil.WriteFile(outputFile, buf, 0x64)
if err != nil {
panic(err.Error())
}
}
func main() {
// PrintToTerminal()
// fmtFmt()
// bufioBufio()
// readFile()
// countInput()
testioutil()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yemingzhi/GolangLearnPractice1.git
git@gitee.com:yemingzhi/GolangLearnPractice1.git
yemingzhi
GolangLearnPractice1
golang练习
2bf136849dce

搜索帮助