Coder Social home page Coder Social logo

go_training's People

Contributors

syuta-sugawara avatar

Watchers

 avatar  avatar  avatar

go_training's Issues

補足: 値レシーバとポインタレシーバ

Structに対して値レシーバーを付ける場合とポインタレシーバを付ける場合で挙動が異なります。
ポインタレシーバが多く使われますが、Immutableなオブジェクトを作成する際には値レシーバーが使われることもあります。

Immutableな例: UUID
こちらの構造体はNewしたあと値を更新しない前提で作られています。
https://godoc.org/github.com/google/uuid#UUID

mutableな例: Image
https://github.com/syuta-sugawara/go_training/blob/master/images.go#L29

imageパッケージ公式でのImage interfaceの実装として、RGBAは値を変更する前提でポインタレシーバーを利用して設計されています。もしこちらが値レシーバーで設計されてしまっていると、以下のSetのようなFunctionは利用できません。
https://golang.org/pkg/image/#RGBA

func (p *RGBA) Set(x, y int, c color.Color)

少し古い記事ですが、以下の記事を読むとより理解が深まるかと思います。
https://skatsuta.github.io/2015/12/29/value-receiver-pointer-receiver/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.