gpt4 book ai didi

Go: (运算符 + 未在 slice 上定义)

转载 作者:IT王子 更新时间:2023-10-29 01:55:41 26 4
gpt4 key购买 nike

我正在尝试编写一个程序来获取用户输入标志,读取包含数据的输入文件 (testInput.txt),然后将用户的标志 append 到输入数据并将其全部导出到输出文件 (testOutput.txt)。尝试将两者 append 在一起时,我在 func employeeReadWrite() 中遇到错误。 “无效操作:内容 + cnvUserProfile(运算符 + 未在 slice 上定义)”。我是编程的新手,go 是我的第一语言,而且我还没有很好地掌握 slice 。我需要做什么来解决该错误?

package main

import (
"flag"
"fmt"
"io/ioutil"
"os"
)

var targetEmployee *string
var targetUsername *string
var targetLocation *string
var targetDepartment *string
var targetManager *string
var targetTitle *string
var userProfile string

func getFlagVariables() {
targetEmployee = flag.String("employee", "", "What is there name? (-employee)")
targetUsername = flag.String("username", "", "What is there username? (-username)")
targetLocation = flag.String("location", "", "Where are the working from? (-location)")
targetDepartment = flag.String("department", "", "What is there department? (-department)")
targetManager = flag.String("manager", "", "Who is there manager? (-manager)")
targetTitle = flag.String("title", "", "What is there job title? (-title)")

flag.Parse()

fmt.Println("-----------------------------------")
userProfile = *targetEmployee + "\n" + *targetUsername + "\n" + *targetUsername + "@genericCompany.com" + "\n" + *targetLocation + "\n" + *targetDepartment + "\n" + *targetManager + "\n" + *targetTitle
fmt.Println(userProfile)
fmt.Println("-----------------------------------")

}

func employeeReadWriteFile() {
contents, _ := ioutil.ReadFile("testInput.txt")
cnvrtUserProfile := []byte(userProfile)
ioutil.WriteFile("testOutput.txt", contents+cnvrtUserProfile, 0x777)
}

最佳答案

您不能在 slice 上使用 +,但是您可以使用 append:

 ioutil.WriteFile("testOutput.txt", append(contents, cnvrtUserProfile), 0x777)

关于Go: (运算符 + 未在 slice 上定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958569/

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