英文:
Go: (operator + not defined on slice)
问题
我正在尝试编写一个程序,该程序接受用户输入的标志,读取包含数据的输入文件(testInput.txt),然后将用户的标志附加到输入数据中,并将所有内容导出到输出文件(testOutput.txt)。在函数employeeReadWrite()中,当我尝试将这两者连接在一起时,出现了一个错误:"invalid operation: contents + cnvUserProfile(切片上未定义的运算符+)"。作为我的第一种编程语言,我对切片的使用还不太熟悉。我需要做什么来解决这个错误?
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 their name? (-employee)")
targetUsername = flag.String("username", "", "What is their username? (-username)")
targetLocation = flag.String("location", "", "Where are they working from? (-location)")
targetDepartment = flag.String("department", "", "What is their department? (-department)")
targetManager = flag.String("manager", "", "Who is their manager? (-manager)")
targetTitle = flag.String("title", "", "What is their 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", append(contents, cnvrtUserProfile...), 0x777)
}
在employeeReadWriteFile()
函数中,将contents
和cnvrtUserProfile
连接在一起时,使用append()
函数将它们附加到contents
切片中。
英文:
Im trying to write a program that takes user input flags, reads an input file containing data (testInput.txt), then appends the user's flags to the input data and exports it all to an output file (testOutput.txt). I am getting an error in func employeeReadWrite() when trying to append the two together. "invalid operation: contents + cnvUserProfile (operator + not defined on slice)". I'm new to programming with go being my 1st language, and I don't have a great handle on slices yet. What do I need to do to resolve that error?
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)
}
答案1
得分: 5
你不能在切片上使用+
运算符,但是你可以使用append
函数:
ioutil.WriteFile("testOutput.txt", append(contents, cnvrtUserProfile), 0x777)
英文:
You can't use +
on a slice, however you can use append
:
ioutil.WriteFile("testOutput.txt", append(contents, cnvrtUserProfile), 0x777)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论