GolangNote

Golang笔记

goalng 利用io.WriteString 把string 写入文件

Permalink

把字符串写入文件,最常用的功能

Go: 利用io.WriteString 把string 写入文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	filename := "test.txt"
	file, err := os.Create(filename)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(" Write to file : " + filename)

	n, err := io.WriteString(file, " Hello World ! 你好")
	if err != nil {
		fmt.Println(n, err)
	}

	file.Close()
}

本文网址: https://golangnote.com/topic/204.html 转摘请注明来源

Related articles

Write a Comment to "goalng 利用io.WriteString 把string 写入文件"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms