golang/go · error

argument %q contains both single and double quotes and canno

Error message

argument %q contains both single and double quotes and cannot be quoted

What it means

Thrown by the Join function when an argument contains both single-quote (') and double-quote (") characters. Join tries to wrap the argument in single quotes if it contains a double quote, or double quotes if it contains a single quote. If both are present, neither quoting strategy can produce a reversible result, so the function refuses to encode it.

Source

Thrown at src/cmd/internal/quoted/quoted.go:99

				sawDoubleQuote = true
			}
		}
		switch {
		case !sawSpace && !sawSingleQuote && !sawDoubleQuote:
			buf = append(buf, arg...)

		case !sawSingleQuote:
			buf = append(buf, '\'')
			buf = append(buf, arg...)
			buf = append(buf, '\'')

		case !sawDoubleQuote:
			buf = append(buf, '"')
			buf = append(buf, arg...)
			buf = append(buf, '"')

		default:
			return "", fmt.Errorf("argument %q contains both single and double quotes and cannot be quoted", arg)
		}
	}
	return string(buf), nil
}

// A Flag parses a list of string arguments encoded with Join.
// It is useful for flags like cmd/link's -extldflags.
type Flag []string

var _ flag.Value = (*Flag)(nil)

func (f *Flag) Set(v string) error {
	fs, err := Split(v)
	if err != nil {
		return err
	}
	*f = fs[:len(fs):len(fs)]
	return nil

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Remove one type of quote character from the argument before calling Join.
  2. Replace embedded quotes with escaped alternatives or use a different delimiter strategy.
  3. If both quote types are essential, avoid quoted.Join and use a custom serialization with proper escaping.

Example fix

// Before (broken): quoted.Join([]string{`it's a "test"`})
// After (fixed):  quoted.Join([]string{`it\'s a test`})
// (remove one quote type so Join can wrap the other)
Defensive patterns

Strategy: validation

Validate before calling

// Check for mixed quotes before calling Join:
func canQuote(arg string) bool {
    hasSingle := strings.Contains(arg, "'")
    hasDouble := strings.Contains(arg, "\"")
    return !(hasSingle && hasDouble)
}

// Usage:
// for _, arg := range args {
//     if !canQuote(arg) {
//         return fmt.Errorf("cannot quote argument with mixed quotes: %q", arg)
//     }
// }

Prevention

When it happens

Trigger: An argument string in the slice passed to Join contains at least one ' character AND at least one " character. The function checks sawSingleQuote and sawDoubleQuote flags; when both are true and neither single-quote-only nor double-quote-only branches apply, the default case fires.

Common situations: Building linker flags or shell command strings where an argument legitimately contains mixed quote characters — e.g. a path or value with embedded SQL-like strings, JSON snippets, or localized text. Rare in practice but unfixable without escaping support.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/c921c97505952f14. Report an issue: GitHub.