I don't have much experience in Go, but basically I want to print my regex on the screen after using it. I cant find anything on Google. This seems something rather easy to do but I have tried several things and nothing else worked.
var swagger_regex = regexp.MustCompile(`[0-9][.][0-9]`)
.... some code here ....
fmt.Println("Your '_.swagger' attribute does not match " + string(swagger_regex))
The regexp.Regexp
type has a Regexp.String()
method which does this exactly:
String returns the source text used to compile the regular expression.
You don't even have to call it manually, as the fmt
package checks and calls the String()
method if the type of the passed value has it.
Example:
r := regexp.MustCompile(`[0-9][.][0-9]`)
fmt.Println("Regexp:", r)
// If you need the source text as a string:
s := r.String()
fmt.Println("Regexp:", s)
Output (try it on the Go Playground):
Regexp: [0-9][.][0-9]
Regexp: [0-9][.][0-9]