英文:
Go channels and I/O
问题
首先,我将为你翻译代码部分:
第一个函数
ReadF2C
接受一个文件名和通道,从文件中读取内容并输入到通道中。
第二个函数
WriteC2F
接受两个通道和一个文件名,获取每个通道的值,并将较小的值保存到输出文件中。我确定代码中有一些语法错误,但我对GO语言还不熟悉。
package main
import (
"fmt"
"bufio"
"os"
"strconv"
)
func main() {
fmt.Println("Hello World!\n\n")
cs1 := make(chan int)
var nameinput string = "input.txt"
readF2C(nameinput, cs1)
cs2 := make(chan int)
cs3 := make(chan int)
cs2 <- 10
cs2 <- 16
cs2 <- 7
cs2 <- 2
cs2 <- 5
cs3 <- 8
cs3 <- 15
cs3 <- 14
cs3 <- 1
cs3 <- 6
var nameoutput string = "output.txt"
writeC2F(nameoutput, cs2, cs3)
}
func readF2C(fn string, ch chan int) {
f, err := os.Open(fn)
r := bufio.NewReader(f)
for err != nil { // not end of file
fmt.Println(r.ReadString('\n'))
ch <- r.ReadString('\n')
}
if err != nil {
fmt.Println(r.ReadString('\n'))
ch <- -1
}
}
func writeC2F(fn string, // output text file
ch1 chan int, // first input channel
ch2 chan int) {
var j int = 0
var channel1temp int
var channel2temp int
f, _ := os.Create(fn)
w := bufio.NewWriter(f)
channel1temp = <-ch1
channel2temp = <-ch2
for j := 1; j <= 5; j++ {
if channel2temp < channel1temp {
n4, err := w.WriteString(strconv.Itoa(channel1temp))
} else {
n4, err := w.WriteString(strconv.Itoa(channel2temp))
}
w.Flush()
}
}
这是我收到的错误信息:
prog.go:38: 在单值上下文中使用了多个值 r.ReadString()
prog.go:65: w.flush 未定义(无法引用未导出的字段或方法 bufio.(*Writer)."".flush)
英文:
First function
> ReadF2C
takes a filename and channel, reads from file and inputs in channel.
Second function
> WriteC2F
takes 2 channels and filename, takes value of each channel and saves the lower value in the output file. I'm sure there is a few syntax errors but i'm new to GO
package main
import (
"fmt"
"bufio"
"os"
"strconv"
)
func main() {
fmt.Println("Hello World!\n\n")
cs1 := make (chan int)
var nameinput string = "input.txt"
readF2C(nameinput,cs1)
cs2 := make (chan int)
cs3 := make (chan int)
cs2 <- 10
cs2 <- 16
cs2 <- 7
cs2 <- 2
cs2 <- 5
cs3 <- 8
cs3 <- 15
cs3 <- 14
cs3 <- 1
cs3 <- 6
var nameoutput string = "output.txt"
writeC2F (nameoutput,cs2,cs3)
}
func readF2C (fn string, ch chan int){
f,err := os.Open(fn)
r := bufio.NewReader(f)
for err != nil { // not end of file
fmt.Println(r.ReadString('\n'))
ch <- r.ReadString('\n')
}
if err != nil {
fmt.Println(r.ReadString('\n'))
ch <- -1
}
}
func writeC2F(fn string, // output text file
ch1 chan int, // first input channel
ch2 chan int){
var j int = 0
var channel1temp int
var channel2temp int
f,_ := os.Create(fn)
w := bufio.NewWriter(f)
channel1temp = <-ch1
channel2temp = <-ch2
for j := 1; j <= 5; j++ {
if (channel2temp < channel1temp){
n4, err := w.WriteString(strconv.Itoa(channel1temp))
} else{
n4, err := w.WriteString(strconv.Itoa(channel2temp))
}
w.flush()
}
}
This is the error messages I get:
prog.go:38: multiple-value r.ReadString() in single-value context
prog.go:65: w.flush undefined (cannot refer to unexported field or method bufio.(*Writer)."".flush)
答案1
得分: 2
有多个错误:
1)
与C语言不同,Go语言要求在语句后面直接使用大括号。所以对于if语句(以及func语句),不要像这样写:
if (channel2temp < channel1temp)
{
而是写成:
if channel2temp < channel1temp {
2)
Go语言中没有while
循环,可以使用for
循环代替:
for {
...
}
或者
for channel1temp != null || channel2temp != null {
...
}
3)
使用未声明的变量。通常可以通过在初始化变量时使用短变量声明来解决。所以不要写成:
r = bufio.NewReader(file)
而是写成:
r := bufio.NewReader(file)
4)
尝试将多个返回值赋值给单个变量。如果一个函数返回两个值,而你只需要其中一个,可以通过将不需要的变量赋值给_
来丢弃它。所以不要写成:
file := os.Open(fn)
而是写成:
file, _ := os.Open(fn)
但最佳实践是捕获错误:
file, err := os.Open(fn)
if err != nil {
panic(err)
}
除此之外还有更多错误,但这些可能会帮助你入门。我还建议阅读Effective Go,因为它会解释我刚才提到的许多内容。
编辑:
当然有在线帮助。虽然Go是一门新语言,但在线资料非常有用。以下是我在学习Go时使用的一些资源:
- Effective Go:关于如何编写符合惯用法的Go代码的优秀文档
- The Go programming language Tour:带有交互式示例的Go在线教程。
- Go By Example:Go程序的交互式示例,从Hello World开始。
- Go Specification:出人意料地易读,因为它是一份规范。也许不是一个起点,但非常有用。
英文:
There are multiple errors:
1)
Unlike C, Go enforces you to have your curly braces directly after your statements. So for an if case (and the same for func), instead of doing it like this:
if (channel2temp < channel1temp)
{
use this
if channel2temp < channel1temp {
2)
There is no while
in Go. Use for
for {
...
}
or
for channel1temp != null || channel2temp != null {
...
}
3)
Usage of non-declared variables. Often easy to fix by making a short variable declaration the first time you initialize the variable. So instead of:
r = bufio.NewReader(file)
use
r := bufio.NewReader(file)
4)
Trying to a assign multi-value return into a single variable. If a function returns two values and you only need one, the variable you don't want can be discarded by assigning it to _
. So instead of:
file := os.Open(fn)
use
file, _ := os.Open(fn)
but best practice would be to catch that error:
file, err := os.Open(fn)
if err != nil {
panic(err)
}
There are more errors on top of this, but maybe it will get you started.
I also suggest reading Effective Go since it will explain many of the things I've just mentioned.
Edit:
And there are help online for sure. It might be a new language, but the online material is really useful. Below is a few that I used when learning Go:
- Effective Go: Good document on how to write idiomatic Go code
- The Go programming language Tour: Online tour of Go with interactive examples.
- Go By Example: Interactive examples of Go programs, starting with Hello World.
- Go Specification: Surprisingly readable for being a specification. Maybe not a start point, but very useful.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论