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

Go func(int)字符串的间接类型无效,go,fizzbuzz,Go,Fizzbuzz,我被以下错误绊倒了: ./main.go:76: invalid indirect of Fizzbuzz (type func(int) string) 我知道Fizzbuzz函数不满足writeString。我的直觉告诉我,这可能是因为我应该使用一个界面来嘶嘶作响?有人能给我一些如何执行的指导吗?我该怎么做才能使这段代码变得习惯化 // -------------------------------INPUT-------------------------------------- /

我被以下错误绊倒了:

./main.go:76: invalid indirect of Fizzbuzz (type func(int) string)
我知道Fizzbuzz函数不满足writeString。我的直觉告诉我,这可能是因为我应该使用一个界面来嘶嘶作响?有人能给我一些如何执行的指导吗?我该怎么做才能使这段代码变得习惯化

// -------------------------------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)
}

*
作为一元运算符用于取消指针的引用(或“间接”)
Fizzbuzz
是一个函数,而不是指针。这就是为什么编译器会说:

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

您真正想做的是调用函数:
Fizzbuzz()

So行:

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

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


将writestring的第一次返回称为fizzbuzz之类的东西不是很习惯用法。通常我们把它命名为“n”


“调用第一次返回不是很习惯用语……通常我们将其命名为n.”——谢谢。@Alex,我刚刚意识到我对这两行都有同样的错误代码。刚刚更新过。
if n, err := w.WriteString(Fizzbuzz()); err != nil{