Created
March 6, 2021 19:28
-
-
Save juusechec/9a6a43586b0fa97c0cdae46e655d2034 to your computer and use it in GitHub Desktop.
GO NumericInput example with rune input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "unicode" | |
type UserInput interface { | |
Add(rune) | |
GetValue() string | |
} | |
type NumericInput struct { | |
input string | |
} | |
func (ni *NumericInput) Add(input rune) { | |
if unicode.IsDigit(input) { | |
ni.input += string(input) | |
} | |
} | |
func (ni *NumericInput) GetValue() string { | |
return ni.input | |
} | |
func main() { | |
var input UserInput = &NumericInput{} | |
input.Add(rune('1')) | |
input.Add(rune('a')) | |
input.Add(rune('0')) | |
fmt.Println(input.GetValue()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment