Handling Semicolons in CSV to Prevent Splitting Across Cells in Go

I am generating a CSV file in a Go HTTP handler and sending it as a response. Some of my text fields contain semicolons (;), which causes issues when opening the file in applications like Excel. The data is getting split into multiple cells instead of appearing in a single cell.

For example, in my case:

  • The name field contains google;gmail
  • The URL field contains https://www.goo;gle.com/

Here’s my current Go code:

func routeReferencesCrossrefDownloadPost(w http.ResponseWriter, r *http.Request) {
	var req DownloadRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		fmt.Println("JSON Decode Error:", err)
		http.Error(w, "Invalid request", http.StatusBadRequest)
		return
	}

	w.Header().Set("Content-Type", "text/csv")
	w.Header().Set("Content-Disposition", `attachment; filename="search_items.csv"`)

	writer := csv.NewWriter(w)
	defer writer.Flush()

	writer.Write([]string{"#", "Name", "URL"})

	name := "google;gmail"
	URL := "https://www.goo;gle.com/"
	for i := 0; i < 5; i++ {
		writer.Write([]string{
			fmt.Sprintf("%d", i+1),
			name,
			URL,
		})
	}
}

The semicolon in name and URL is causing the text to split across multiple cells instead of staying within one. I would prefer not to change any CSV settings in system. If I wrap values in double quotes ("), I should not see them explicitly in the CSV file when opened.

How can I ensure that values containing semicolons remain in the same cell without requiring the user to modify any settings in their CSV viewer?

It seems you are using semicolon as field separator. Try using comma as field separator (I am not sure but I think it is used by default)