Explorar o código

New now returns captcha with default length.

NewLen is now like the old New (accepts captcha length as the argument).
Also, rename StdLength to DefaultLen.
Dmitry Chestnykh %!s(int64=15) %!d(string=hai) anos
pai
achega
647026ab1a
Modificáronse 2 ficheiros con 12 adicións e 6 borrados
  1. 11 5
      captcha.go
  2. 1 1
      example/main.go

+ 11 - 5
captcha.go

@@ -50,8 +50,8 @@ import (
 )
 
 const (
-	// Standard number of digits in captcha.
-	StdLength = 6
+	// Default number of digits in captcha solution.
+	DefaultLen = 6
 	// The number of captchas created that triggers garbage collection used
 	// by default store.
 	CollectNum = 100
@@ -84,9 +84,15 @@ func RandomDigits(length int) []byte {
 	return d
 }
 
-// New creates a new captcha of the given length, saves it in the internal
-// storage, and returns its id.
-func New(length int) (id string) {
+// New creates a new captcha with the standard length, saves it in the internal
+// storage and returns its id.
+func New() string {
+	return NewLen(DefaultLen)
+}
+
+// NewLen is just like New, but accepts length of a captcha solution as the
+// argument.
+func NewLen(length int) (id string) {
 	id = uniuri.New()
 	globalStore.Set(id, RandomDigits(length))
 	return

+ 1 - 1
example/main.go

@@ -21,7 +21,7 @@ func showFormHandler(w http.ResponseWriter, r *http.Request) {
 		CaptchaId  string
 		JavaScript string
 	}{
-		captcha.New(captcha.StdLength),
+		captcha.New(),
 		formJavaScript,
 	}
 	if err := formTemplate.Execute(w, &d); err != nil {