Skip to content

Any good examples of custom type scanners/encoders that carry state? #2121

Answered by jackc
andremedeiros asked this question in Q&A
Discussion options

You must be logged in to vote

It's pretty easy to define a new type that transforms its values. For example, if you are strictly encrypting strings, you could implement TextScanner and TextValuer.

Are there any good examples of values that have scanners/encoders that carry state anywhere?

But that's the problem. Your new type could reference a single global key, but it sounds like you need multiple keys.

Here's one approach, you would need some sort of object that had the key that would wrap the underlying value. Something like:

type TextCryptor {
// ...
}

func (c *TextCryptor) Scanner(target string) TextScanner {
// return a TextScanner that decrypts the cyphertext and stores the plaintext in target
}

func (c *Te…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by andremedeiros
Comment options

You must be logged in to vote
0 replies
# for free to join this conversation on GitHub. Already have an account? # to comment
Category
Q&A
Labels
None yet
2 participants