| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package writestring defines an Analyzer that detects |
| 6 | // inefficient string concatenation in uses of WriteString. |
| 7 | // |
| 8 | // # Analyzer writestring |
| 9 | // |
| 10 | // writestring: detect inefficient string concatenation in uses of WriteString |
| 11 | // |
| 12 | // The writestring analyzer offers to replace a call to WriteString(x + y) by |
| 13 | // two calls WriteString(x); WriteString(y). This is more efficient because it |
| 14 | // avoids the additional memory allocation produced by string concatenation; |
| 15 | // instead we just write each string into the buffer directly. |
| 16 | // |
| 17 | // It explicitly looks for calls to certain well-known writers such as |
| 18 | // bytes.Buffer, strings.Builder and bufio.Writer. The analyzer will not suggest |
| 19 | // a fix for calls to, say, (*os.File).WriteString, because for certain kinds of |
| 20 | // file such as a UDP socket, it could split a single message into two. |
| 21 | // Similarly it does not offer fixes when the type of the writer is unknown (as |
| 22 | // in calls to io.WriteString). |
| 23 | // |
| 24 | // For example: |
| 25 | // |
| 26 | // func f(a string, b string) string { |
| 27 | // var s strings.Builder |
| 28 | // s.WriteString(a+b) |
| 29 | // return s.String() |
| 30 | // } |
| 31 | // |
| 32 | // would become: |
| 33 | // |
| 34 | // func f(a string, b string) string { |
| 35 | // var s strings.Builder |
| 36 | // s.WriteString(a) |
| 37 | // s.WriteString(b) |
| 38 | // return s.String() |
| 39 | // } |
| 40 | package writestring |