duanguan3863 2013-12-16 04:35
浏览 44

类型为func(int)的无效的间接字符串

I'm getting stucked with the following error:

./main.go:76: invalid indirect of Fizzbuzz (type func(int) string)

I understand that the Fizzbuzz function does not satisfy the writeString. My intuition is telling me that this is probably because I should be using an interface to Fizzbuzz? Can someone please give me some direction on how to execute this? What can I do to make this code Go idiomatic?

// -------------------------------INPUT--------------------------------------

// Your program should read an input file (provided on the command line),
// which contains multiple newline separated lines.
// Each line will contain 3 numbers which are space delimited.
// The first number is first number to divide by ('A' in this example),
// the second number is the second number to divide by ('B' in this example)
// and the third number is where you should count till ('N' in this example).
// You may assume that the input file is formatted correctly and the
// numbers are valid positive integers. E.g.

// 3 5 10
// 2 7 15

// -------------------------------OUTPUT------------------------------------

// Print out the series 1 through N replacing numbers divisible by 'A' by F,
// numbers divisible by 'B' by B and numbers divisible by both as 'FB'.
// Since the input file contains multiple sets of values, your output will
// print out one line per set. Ensure that there are no trailing empty spaces
// on each line you print. E.g.

// 1 2 F 4 B F 7 8 F B
// 1 F 3 F 5 F B F 9 F 11 F 13 FB 15

// ---------------------------PROPOSED SOLUTION-----------------------------

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func Fizzbuzz(N int) (output string) {
    var (
        A = N%3 == 0
        B = N%5 == 0
    )

    switch {
    case A && B:
        output = "FB"

    case A:
        output = "F"

    case B:
        output = "B"

    default:
        output = fmt.Sprintf("%v", N)

    }
    return
}

func openFile(name string) *os.File {
    file, err := os.Open(name)
    if err != nil {
        log.Fatalf("failed opening %s for writing: %s", name, err)
    }
    return file
}

func Readln(r *bufio.Reader) {
    line, prefix, err := r.ReadLine()
    if err != nil {
        log.Fatalf("failed reading a line: %v", err)
    }
    if prefix {
        log.Printf("Line is too big for buffer, only first %d bytes returned", len(line))
    }
}

func WriteString(w *bufio.Writer) {
    if n, err := w.WriteString(*Fizzbuzz); err != nil {
        log.Fatalf("failed writing string: %s", err)
    } else {
        log.Printf("Wrote string in %d bytes", n)
    }
}

func main() {
    file := openFile(os.Args[1])
    defer file.Close()

    fi := bufio.NewReader(file)
    Readln(fi)

    fo := bufio.NewWriter(file)
    defer fo.Flush()

    WriteString(fo)
}

Go-Playground

  • 写回答

1条回答 默认 最新

  • doufu5747 2013-12-16 05:14
    关注

    * as a unary operator is used to dereference (or "indirect") a pointer. Fizzbuzz is a function, not a pointer. That is why the compiler says:

    Invalid indirect of type func (int) string

    What you really want to do is call the function: Fizzbuzz()

    So line:

    if fizzbuzz, err := w.WriteString(*Fizzbuzz); err != nil {
    

    should be:

    if fizzbuzz, err := w.WriteString(Fizzbuzz()); err != nil{
    

    It is not very idiomatic to call the first return of writestring something like fizzbuzz. Normally we name it "n".

    if n, err := w.WriteString(Fizzbuzz()); err != nil{
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器