3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

名前付きstring型をScanしたい時(database.sql.Scanner)

Last updated at Posted at 2016-01-19
package main

import "fmt"

type Animal string

const (
	Cat Animal = "cat"
	Dog Animal = "dog"
)

func (a *Animal) Scan(src interface{}) error {
	str := fmt.Sprintf("%s", src)
	*a = Animal(str)
	return nil
}

func main() {
	var animal Animal
	animal.Scan("cat")
	fmt.Println(animal)

	fmt.Println(animal.Scan("monkey"))
}

run:

cat
<nil>

Playground

よく忘れるので・・・

参考

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?