gpt4 book ai didi

file - 尝试使用 io.WriteString 写入文件时出现 Golang "Access is denied"错误

转载 作者:IT王子 更新时间:2023-10-29 01:51:20 27 4
gpt4 key购买 nike

我目前运行的是 Windows 8 64 位系统,我正在尝试创建一个用于 Web 服务器的日志记录文件。有问题的代码是:

func LogWebPath(requestedURL string, accessedURL string, logFile string) error {

file, _ := os.Open(logFile)
_, err = io.WriteString(file, requestedURL + ":" + accessedURL)
if(err != nil) {
fmt.Println(err)
return err
}
file.Close()
return errors.New("nil")
}

每当调用 io.WriteString 时,返回的错误是 write log/visit.log: Access is denied.

我的系统上安装了 Go,我正在使用 go run x.go 来运行我的 Go 源代码。

最佳答案

我相信您正在以只读模式打开文件。您可以尝试使用适当的标志代替 os.Open,如 How to append text to a file in golang? 所示。和 Append to a file in Go

关于file - 尝试使用 io.WriteString 写入文件时出现 Golang "Access is denied"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967717/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com