Go通道和I/O

huangapple go评论92阅读模式
英文:

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 (
&quot;fmt&quot;
&quot;bufio&quot;
&quot;os&quot;
&quot;strconv&quot;
)
func main() {
fmt.Println(&quot;Hello World!\n\n&quot;)
cs1 := make (chan int)
var nameinput string = &quot;input.txt&quot;
readF2C(nameinput,cs1)
cs2 := make (chan int)
cs3 := make (chan int)
cs2 &lt;- 10
cs2 &lt;- 16
cs2 &lt;- 7
cs2 &lt;- 2
cs2 &lt;- 5
cs3 &lt;- 8
cs3 &lt;- 15
cs3 &lt;- 14
cs3 &lt;- 1
cs3 &lt;- 6
var nameoutput string = &quot;output.txt&quot;
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(&#39;\n&#39;))
ch &lt;- r.ReadString(&#39;\n&#39;)
}
if err != nil {
fmt.Println(r.ReadString(&#39;\n&#39;))
ch &lt;- -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 = &lt;-ch1
channel2temp = &lt;-ch2
for j := 1; j &lt;= 5; j++ {
if (channel2temp &lt; 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).&quot;&quot;.flush)

答案1

得分: 2

有多个错误:

1)

与C语言不同,Go语言要求在语句后面直接使用大括号。所以对于if语句(以及func语句),不要像这样写:

if (channel2temp &lt; channel1temp)
{

而是写成:

if channel2temp &lt; 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时使用的一些资源:

英文:

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 &lt; channel1temp)
{

use this

if channel2temp &lt; 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:

huangapple
  • 本文由 发表于 2014年4月7日 06:26:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/22900778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定