Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Go中逐行处理文件?_Go - Fatal编程技术网

如何在Go中逐行处理文件?

如何在Go中逐行处理文件?,go,Go,在这里,我试图编写一个函数findMajordInterference(words),它返回文件“words.txt”中10、11和12字符串的单词。k字串是任何一对不同字母(在字母表的圆形排列中)之间的距离大于k的字串B。比如说, “丝”是一串 “oaks”是一个3字符串、2字符串和1字符串 在下面的代码中,我试图将所有10-、11-和12-字符串放入一个数组中,但我认为它有问题。我一直在试图弄清楚我是否正确地逐行处理了文件 package main import ( "fmt"

在这里,我试图编写一个函数findMajordInterference(words),它返回文件“words.txt”中10、11和12字符串的单词。k字串是任何一对不同字母(在字母表的圆形排列中)之间的距离大于k的字串B。比如说,

  • “丝”是一串
  • “oaks”是一个3字符串、2字符串和1字符串
在下面的代码中,我试图将所有10-、11-和12-字符串放入一个数组中,但我认为它有问题。我一直在试图弄清楚我是否正确地逐行处理了文件

package main

import (
    "fmt"
    "io/ioutil"
)

func findMajorDifference(words []byte) []string 
{
    alpha := "abcdefghijklmnopqrstuvwxyz"
    major := []string{}

    B := string(words)

    distance := 0 // current distance between 2 distinct letters (pos1 - pos2)
    min := 26     // smallest distance between 2 distinct letters
    pos1 := 0     // position of first letter in the alpha array
    pos2 := 0     // position of second letter in the alpha array

    for i := 0; i < len(B); i++ {
        current := B[i] // current letter
        for x := 1; x < len(B); x++ {
            next := B[x] // next distinct letter
            if current != next {
                // find position of letters
                for j := 0; j < len(alpha); j++ {
                    if current == alpha[j] {
                        pos1 = j
                    }
                }
                for k := 0; k < len(alpha); k++ {
                    if next == alpha[k] {
                        pos2 = k
                    }
                }
                // find distance
                if pos1 > pos2 {
                    distance = pos1 - pos2
                } else {
                    distance = pos2 - pos1
                }
                if distance < min {
                    min = distance
                }
            }
        }
        if min == 11 || min == 12 || min == 13 {
            major = append(major, string(B[i]))
        }
    }
    return major
} // end of findMajorBinjai

func main() 
{
    words, err := ioutil.ReadFile("words.txt")

    if err != nil {
        fmt.Println("File reading error", err)
        return
    }

    fmt.Println("test")                           // This line is printed
    fmt.Println("%s", findMajorDifference(words)) // Gives no output

}
主程序包
进口(
“fmt”
“io/ioutil”
)
func findMajordInterference(字[]字节)[]字符串
{
alpha:=“abcdefghijklmnopqrstuvxyz”
主要:=[]字符串{}
B:=字符串(字)
距离:=0//两个不同字母之间的当前距离(pos1-pos2)
最小:=26//两个不同字母之间的最小距离
pos1:=0//第一个字母在alpha数组中的位置
pos2:=0//alpha数组中第二个字母的位置
对于i:=0;ipos2{
距离=位置1-位置2
}否则{
距离=位置2-位置1
}
如果距离
我的代码没有给出任何错误,但也没有打印出我想要的输出。

使用并


根据您的解释,文件中有多个单词。在当前代码段中执行此操作的方式是,将文件的完整内容作为bite数组传递。如果文件中只有一个单词,这将很好地工作。如果有多个单词,在计算距离时也会考虑不同单词的字母。当返回空白切片时,您不会得到任何输出

您可以通过单独阅读每个单词并将其传递给函数来克服此问题。我已经修改了你的主要功能,在不改变功能的情况下达到了预期的效果

func main() {
// words, err := ioutil.ReadFile("words.txt")
filehandle, err := os.Open("words.txt")
if err != nil {
    panic(err)
}
defer filehandle.Close()
scanner := bufio.NewScanner(filehandle)
for scanner.Scan() {
    words := scanner.Text()
    fmt.Println("%s", findMajorDifference([]byte(words))) // Gives no output
}

}

至于“silk”,我们需要找到任何一对不同字母之间的距离,因此:SI=10,SL=7,SK=8,IL=3,IK=2,KL=2。因为计算的最小距离是2,这意味着“丝绸”是一个1字符串。希望这有帮助!至于“橡树”:OA=12,OK=4,OS=4,AK=10,as=8,KS=8。计算的最小距离为4,因此适用于3串、2串和1串。这里有更多的例子:“gggaaaccccaceegg”是一个1字符串;“gggaaaddggggwwwwaaaaaaaaaaaggggggggg”是一个2字符串,也是一个1字符串。二元运算符⊖ 表示字母3的圆形排列中两个字母之间的距离。例如,“c”⊖ ‘y'='y'⊖ ‘c'=4。因为字母表中有26个字母,所以对于任何字母对a和b,0≤ A.⊖ B≤ 13.@NAmilah:问题似乎在于,函数是用来计算word的逻辑的,而文件的全部内容是作为main的参数传递的。我已经修改了我的答案,包括可能解决您的主要问题issue@NAmilah:答案是否有助于实现您的目标?答案可能重复
func main() {
// words, err := ioutil.ReadFile("words.txt")
filehandle, err := os.Open("words.txt")
if err != nil {
    panic(err)
}
defer filehandle.Close()
scanner := bufio.NewScanner(filehandle)
for scanner.Scan() {
    words := scanner.Text()
    fmt.Println("%s", findMajorDifference([]byte(words))) // Gives no output
}