为什么在Go中合并这两个字符串会使结尾的3个字符移动到组合字符串的前3个字符之上?

When trying to concatenate two strings, they combine but the next three characters overwrite earlier concatenated text before again continuing to concatenate as expected. I suspect this is something to do with the retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap) function as this phenomena only happens when it is called within the double for of the cmd.Interaction section of build_executable.

Where the error is

func build_executable(cmd shell_command, var_swap string_matrix, is_first bool) string{
    sleep_duration := cmd.Sleep
    result := ""
    if !is_first{
        result = "send \""
    } else {
        result = "spawn "
    }
    result += cmd.Command
    if len(cmd.Options.Dashes) > 0 {
        for index := range cmd.Options.Dashes{
            if cmd.Options.Dashes[index] != "" || cmd.Options.Flags[index] != ""{
                result += " " + cmd.Options.Dashes[index] + cmd.Options.Flags[index]
            }
            if cmd.Options.Values[index] != ""{
                result += " " + cmd.Options.Values[index]
            }
        }
    }
    if len(cmd.Targets.Litteral) > 0{
        result += " "
        for index := range cmd.Targets.Litteral{
            result += cmd.Targets.Litteral[index] + retrieve_mapped_value(cmd.Targets.Variable[index], var_swap)
        }
    }
    if !is_first{
        result += "\\"
"
    } else {
        result += "
"
    }
    result += "sleep " + sleep_duration + "
"
    if len(cmd.Interaction.Prompts) > 0{
        for p_index := range cmd.Interaction.Prompts{
            fmt.Println("cmd.Interaction.Prompts[p_index]\t" + cmd.Interaction.Prompts[p_index])
            result += "expect \"" + cmd.Interaction.Prompts[p_index] + "\"
send \""
            for r_index := range cmd.Interaction.Replies[p_index].Litteral{
                fmt.Println("cmd.Interaction.Replies[p_index].Litteral[r_index]\t'" + cmd.Interaction.Replies[p_index].Litteral[r_index] + "'")
                fmt.Println("cmd.Interaction.Replies[p_index].Variable[r_index]\t'" + cmd.Interaction.Replies[p_index].Variable[r_index] + "'")
                fmt.Println("retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)\t'" + retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap) + "'")
                result += cmd.Interaction.Replies[p_index].Litteral[r_index] 
                result += retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)
                result += "" + "" + ""
            }
            result += "\\"
sleep " + sleep_duration + "
"
        }
    }
    if cmd.Expects != "" {
        result += "expect \"" + cmd.Expects + "\"
"
    }
    return result
}

Suspect function

func retrieve_mapped_value(key string, mapping string_matrix) string{
    if key != "" {
        for _, layer := range mapping{
            if layer[0] == key {
                return layer[1]
            }
        }
    } else {
        return key
    }
    return "***No Match Error***"
}

What I should get

expect "Enter password for user root: "
send "e3H-*HGHu__7"
sleep 10

What I actually get

expect "Enter password for user root: "
"d "e3H-*HGHu__7
sleep 10

It is taking the last 3 characters of one line and overwriting the front with them. I don't understand.

I never found the solution to this but the temp_add section of the below where I arbitrarily slice off the end of the string works. There is some voodoo up in here.

func build_executable(cmd shell_command, var_swap string_matrix, is_first bool) string{
    sleep_duration := cmd.Sleep
    result := ""
    if !is_first{
        result = "send \""
    } else {
        result = "spawn "
    }
    result += cmd.Command
    if len(cmd.Options.Dashes) > 0 {
        for index := range cmd.Options.Dashes{
            if cmd.Options.Dashes[index] != "" || cmd.Options.Flags[index] != ""{
                result += " " + cmd.Options.Dashes[index] + cmd.Options.Flags[index]
            }
            if cmd.Options.Values[index] != ""{
                result += " " + cmd.Options.Values[index]
            }
        }
    }
    if len(cmd.Targets.Litteral) > 0{
        result += " "
        for index := range cmd.Targets.Litteral{
            result += cmd.Targets.Litteral[index] + retrieve_mapped_value(cmd.Targets.Variable[index], var_swap)
        }
    }
    if !is_first{
        result += "\\"
"
    } else {
        result += "
"
    }
    result += "sleep " + sleep_duration + "
"
    if len(cmd.Interaction.Prompts) > 0{
        for p_index := range cmd.Interaction.Prompts{
            interaction_result := "expect \"" + cmd.Interaction.Prompts[p_index] + "\"
send \""
            temp_add := ""
            for r_index := range cmd.Interaction.Replies[p_index].Litteral{
                interaction_result += cmd.Interaction.Replies[p_index].Litteral[r_index]
                temp_add = retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)
                if temp_add != ""{      //this looks wasteful but it is needed for reasons I don't understand
                    temp_add += "
"    //the basic explaination is it seems `retrieve_mapped_value` appends a carriage return & this
                    temp_add = temp_add[:len(temp_add) - 2]     //messes up the script, I don't know why it appends it, but removes it
                }
                interaction_result += temp_add
            }
            interaction_result += "\\"
sleep " + sleep_duration + "
"
            result += interaction_result
        }
    }
    if cmd.Expects != "" {
        result += "expect \"" + cmd.Expects + "\"
"
    }
    return result
}