Koeng101 / dnadesign

A Go package for designing DNA.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change WriterTo to make a builder first

Koeng101 opened this issue · comments

Here is an example written by ChatGPT:

func writeHD(w io.Writer, hd map[string]string) error {
    // Define the order of specific keys
    orderedKeys := []string{"VN", "SO", "GO", "SS"}

    // String builder to accumulate the output
    var sb strings.Builder

    // Write specific keys first if they exist
    for _, key := range orderedKeys {
        if value, exists := hd[key]; exists {
            sb.WriteString(fmt.Sprintf("%s:%s\t", key, value))
        }
    }

    // Write the remaining key-value pairs
    for key, value := range hd {
        // Skip if the key is one of the specific keys
        if key == "VN" || key == "SO" || key == "GO" || key == "SS" {
            continue
        }
        sb.WriteString(fmt.Sprintf("%s:%s\t", key, value))
    }

    // Write to the io.Writer
    _, err := w.Write([]byte(sb.String()))
    return err
}

Basically, accumulate the output in a builder before writing out to the io.Writer. This has advantages, since writing more bytes at once will likely be more efficient than writing super often. It also decreases the amount of tests needed.

This should be implemented across all parsers in a single PR.

This issue has had no activity in the past 2 months. Marking as stale.