douyangqian5243 2014-04-06 22:26
浏览 12
已采纳

转到频道和I / O

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!

")
    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('
'))
    ch <- r.ReadString('
')
    }

    if err != nil {
        fmt.Println(r.ReadString('
'))
            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条回答 默认 最新

  • dshgdhdfcas30210 2014-04-06 22:52
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程