我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

打开和写入文件时出错

I want to open a file and write some text to it, however I get the following error:

.\hello.go:13: cannot use msg (type string) as type []byte in argument to f.Write

Here's my code so far:

package main

import (
    "os"
)

func printer(msg string) (err error) {
    f, err := os.Create("helloworld.txt")
    if err != nil {
        return err
    }
    defer f.Close()
    f.Write(msg)
    return err
}

func main() {
    printer("Hello World")
}

Use io.WriteString(f, msg), f.Write([]byte(msg)) or io.Copy(f, strings.NewReader(msg)).

近期文章

  • Golang解析JSON返回0
  • 在Go中检查字符串指针的长度
  • Type.Field和Value.Field有什么区别?
  • osPathSeparator是符文类型,但想用作字符串
  • 如何在Golang中用C#和Server编写Tcp / ip网络,以进行统一的多人跳棋游戏。
  • HTTP.Server关闭导致运行时错误
  • 在Go Cloud中如何处理状态?
  • Golang中没有返回值函数
  • 如何定义go struct的键和值
  • go get在哪里存储下载的内容?
  • 为什么有时在paren中有返回值,而有时在Go函数签名中却没有?
  • 为什么不能在go中传递`func()[] int`作为`func()[] interface {}`?
  • Golang IPv6服务器
  • 如何将Golang纳入您的网站? [关闭]
  • 如何在运行时更改变量类型
  • 如何在Go中替换字符串中的最后两个'/'字符
  • 如何解析以下格式的日期/时间?
  • 如何使golang打印JSON中的所有字段?
  • golang:使用嵌套结构
  • 与测试一起使用时,方法无法访问全局变量

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华