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. 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
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. 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. 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. 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. 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.
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. 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.
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. 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.
25. Structs and Interfaces
Polymorphism without inheritance
No need to explicitly implement an interface or inherit in order to qualify as an
interface type
33. 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.
34. Go in action
How would we do typical web CRUD apps in Go like we do in Java?
37. Exposing REST API with Spring
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
38. 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/
43. Golang web crud example
https://tutorialedge.net/golang/golang-orm-tutorial/
44. 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
48. 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.
49. 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.
50. 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
53. 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.
54. 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.
55. 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).
56. 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.
57. 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
58. 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
59. 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?
62. 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 ?
64. 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
65. Upside of Go as a developer
Great k8s-related libraries
Simple basic structures, esp. concurrency
Modules are great
Standard library is great
66. 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