MCPcopy
hub / github.com/coder/websocket / randString

Function randString

internal/examples/chat/chat_test.go:250–268  ·  view source on GitHub ↗

randString generates a random string with length n.

(n int)

Source from the content-addressed store, hash-verified

248
249// randString generates a random string with length n.
250func randString(n int) string {
251 b := make([]byte, n)
252 _, err := rand.Reader.Read(b)
253 if err != nil {
254 panic(fmt.Sprintf("failed to generate rand bytes: %v", err))
255 }
256
257 s := strings.ToValidUTF8(string(b), "_")
258 s = strings.ReplaceAll(s, "\x00", "_")
259 if len(s) > n {
260 return s[:n]
261 }
262 if len(s) < n {
263 // Pad with =
264 extra := n - len(s)
265 return s + strings.Repeat("=", extra)
266 }
267 return s
268}
269
270// randInt returns a randomly generated integer between [0, max).
271func randInt(max int) int {

Callers 2

Test_chatServerFunction · 0.85
randMessagesFunction · 0.85

Calls 1

ReadMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…