Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java to Golang: An intro by Ryan Dawson Seldon.io

215 views

Published on

for more information visit: http://salaboy.com

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Java to Golang: An intro by Ryan Dawson Seldon.io

  1. 1. Java to Golang: An Intro Ryan Dawson Seldon LJC 20/02/20
  2. 2. Outline Why Golang? Coming from Java The Golang ethos Golang features Golang examples
  3. 3. Why this presentation? I came to Go after doing Java for 10 years. There are preconceptions you have to force yourself out of. We can’t learn a language in a presentation. We can try to break these preconceptions and make a revealing comparison.
  4. 4. Why Go? Good concurrency features Simplicity of core concepts Great in-built standard libraries Compiles to standalone binary Fast cold start and low memory footprint The language of Kubernetes
  5. 5. Golang for DevOps
  6. 6. Coming from Java Packaging system very different. No maven! Structs look like Java objects but they’re not. No inheritance Scoping very different No Spring! No Spring Boot!
  7. 7. No Spring Boot! But that’s how I choose which libraries to use.... I @Autowired my whole life already... I do my best copy-pasting from Spring Boot examples
  8. 8. It’ll be ok The go standard library is extensive. Includes: - Json and yaml handling - Http/Web server - Templating - Database drivers - … You can achieve encapsulation without Spring Beans There’s lots of good, readable Go code out there
  9. 9. When in go, do as the gophers do “If a language has too many features, you waste time choosing which ones to use.” Rob Pike Clear, maintainable code with low cognitive overhead
  10. 10. Go has a simplicity culture Java loves design patterns. Design patterns mean there’s more than one way. Gophers think Java provides too much freedom, which overcomplicates.
  11. 11. Java Preconception: OO Won Java thinks it is the end of history
  12. 12. Think C
  13. 13. Navigating the Ecosystem If you search for Java webapp examples you’ll probably find lots of struts, JSF, JSP, tomcat, jetty etc. Spring boot provides some standardisation. You can plug things but your API into them is pretty much the same. For Golang, searches tend to immediately give something usable. https://github.com/avelino/awesome-go
  14. 14. This is an Intro Even short programming exercises (e.g. TDD katas) take longer than this presentation. We can’t learn the language here. But we can get an initial feeling for it.
  15. 15. Let’s start simple
  16. 16. Hello Random Number
  17. 17. Functions
  18. 18. Variable Initialization
  19. 19. If-else Checks
  20. 20. Exports and Packages
  21. 21. Modules In the code: import "github.com/username/packagename" From the command-line, in the project: go get -u github.com/username/packagename Updates a go.mod file. The -u forces an update. Or just go build and it automatically updates go.mod and builds the code. That’s basically it.
  22. 22. Modules and versions Reference a tag with go get example.com/hello@v1.0.1-alpha It can get more complex but that’s the core of it. You can import different versions of the same dependency in your app.
  23. 23. Structs
  24. 24. Structs and Interfaces
  25. 25. Structs and Interfaces Polymorphism without inheritance No need to explicitly implement an interface or inherit in order to qualify as an interface type
  26. 26. Go can be weird
  27. 27. Json Encoding
  28. 28. Json Parsing
  29. 29. Web Server
  30. 30. Channels
  31. 31. In Action
  32. 32. Weirdness The interface casting is unusual. More often you’d use a struct. But it shows the flexibility as well as how different this is from jackson. Channels and go-routines look almost too simple but they’re powerful. Sometimes Go is chosen because of them.
  33. 33. Go in action How would we do typical web CRUD apps in Go like we do in Java?
  34. 34. Spring Data Reminder
  35. 35. Spring Data Rest Maven dependency
  36. 36. Exposing REST API with Spring @RepositoryRestResource(collectionResourceRel = "people", path = "people") public interface PersonRepository extends PagingAndSortingRepository<Person, Long> { List<Person> findByLastName(@Param("name") String name); }
  37. 37. Spring Web CRUD Example You don’t do much directly with tomcat or hibernate. They’re abstracted. This is to make them pluggable. Gives a sense of magic though. With Golang you are closer to the project APIs. Let’s see by using https://tutorialedge.net/golang/golang-orm-tutorial/
  38. 38. Golang In-mem DB
  39. 39. Main sets up handlers
  40. 40. List Users
  41. 41. Add User
  42. 42. Golang web crud example https://tutorialedge.net/golang/golang-orm-tutorial/
  43. 43. Comparing Spring boot version much shorter but feels more ‘magic’. Need to know what is going on even if you don’t state it. Apps rarely stay simple. Go version more low-level - json and db handling code explicit Means intent is more explicit Web CRUD is more typical of Java than Go
  44. 44. Testing Mockito is heavily used in Java. Testify in Go has similar functionality but isn’t so dominant.
  45. 45. Mocks in Java: Mockito
  46. 46. Go Testify
  47. 47. Learning Go Java Spring has a whole ethos. We get so embedded we no longer notice it. Go also wants to achieve simplicity and standardisation. But at the language level. Without inheritance or a DI framework. Best to think of Go and Java as different rather than ask which is better, at least while learning.
  48. 48. When Go? Lots of languages do crud web dev fine - python django, ruby, node, java, go. Think about what other projects you’re interacting with and their APIs. Opinion: If you really just need web dev and crud then either use what you prefer or what you can resource for.
  49. 49. Further reading Go In Action The Go Programming Language goroutines https://gobyexample.com/goroutines channels https://gobyexample.com/channels more testing https://medium.com/boldly-going/unit-testing-in-go-with-ginkgo-part-1-ce6ff06eb17f CLI-builder features https://towardsdatascience.com/how-to-create-a-cli-in-golang-with-cobra-d729641c7177 docker, env vars and logs https://www.callicoder.com/docker-golang-image-container-example/ K8s libraries https://www.oreilly.com/library/view/programming-kubernetes/9781492047094/ch04.html awesome go list https://github.com/avelino/awesome-go
  50. 50. REJECTED SLIDES SLIDES FROM HERE WILL NOT BE IN PRESENTATION
  51. 51. Golang for k8s The k8s libraries are among the most used and are well-supported, along with AWS SDK. Go also popular for web development and CLI building (cobra). So a good choice for a web app or CLI that interacts with low-level k8s or other cloud infra.
  52. 52. No Spring Beans! Uses of Spring Beans: - Configurable apps + libraries (@Configuration) - Constructing a kind of API with scoping features (@ComponentScan of packages) - Mock injection Can write good apps with golang’s built-in language features. It doesn’t have to mean any more code for library writers or library users.
  53. 53. Living Without Spring Beans Config can be done with env vars and initialisation methods and params APIs can be well-designed without beans. Exporting helps with scoping (we’ll cover it) Go’s built-in testing support is popular and there are frameworks such as testify (which we’ll look at).
  54. 54. Living without Spring Spring does help produce standardisation. But golang is more opinionated than Java. Spring is more overrideable but I’ve not found myself missing that. You do see more code generators with go - kubebuilder, hugo, cobra. And it’s promoted. There’s no direct equivalent of the spring boot starter. There is a good package management system.
  55. 55. Stop Thinking Spring No under the hood magic controlled by switches in go Much more in the standard library Learn to think go Spring could prob be done in go but it’s not the go ethos
  56. 56. Project Layout and Patterns https://github.com/golang-standards/project-layout Pkg? Maybe that’s too much? Links there look interesting PROBABLY DITCH THIS SLIDE
  57. 57. Treasure example? Compare http examples java and golang Walkthrough? Atomic integer? https://github.com/ryandawsonuk/secrets-treasurehunt Or do something based on calling an online json to yaml service? Or add a separate thing on goroutines and locks?
  58. 58. Json Parsing https://gobyexample.com/json shows interface and casting
  59. 59. Web Calls
  60. 60. Web Calls https://words.bighugelabs.com/api/sample/json gives some example json (more readable in a json formatter) https://github.com/matryer/goblueprints/blob/master/chapter4/thesaurus/bighuge.go selectively reads parts of it into a struct Or use https://gobyexample.com/http-clients ?
  61. 61. Sidenote: Type embedding not inheritance
  62. 62. Choosing packages I first look at standard library, then a relevant open source project then look at what’s popular. Others do similar. A good reference is https://github.com/avelino/awesome-go
  63. 63. Upside of Go as a developer Great k8s-related libraries Simple basic structures, esp. concurrency Modules are great Standard library is great
  64. 64. Downsides of Go Not everyone on modules yet and they’re kinda github-centric At the fringes choosing libraries does get tricky Takes some getting used to when coming from java! Error handling is a bit verbose but that’s being looked at Generics would be nice and are being considered

×