Livestreams Tutorials

Video Recording and Q&A of Build a Microservice in Go with GoLand in an Hour webinar

Last week we hosted our first webinar, with the theme of Go Microservices development in GoLand. It was great to see so many people attending, asking questions, and giving us their feedback. To that end, I’d like to say a big thank you to everyone who participated, as well as to our JetBrains team that made this all possible.

During the webinar, we promised to reply to as many questions as we can, and that’s what we’re doing in today’s blog post.

Here’s a quick recap of the presentation:

  • We used a basic “Hello World” application using net/http as a base project.
  • We then deployed a database in a Kubernetes cluster.
  • We linked the IDE to the database, allowing us to index the database structure and provide code assistance later.
  • We modified the application to connect to the database.
  • We deployed the application to the Kubernetes cluster and debugged it.
  • We refactored the application to make it easier to test and organize the source code.
  • We used code generation to create tests for our application and prevent bugs in the future.
  • Q&A.

If you missed the webinar, don’t worry, you can find a recording of it here:

The source code created during the presentation can be found here: https://github.com/dlsniper/go-microservice-webinar

From the questions we received, we picked three respondents to receive a free license. We’ll be sending those out to the lucky winners shortly.

We divided the questions into four categories:

Regarding the environment used in the presentation:

Gopher: Is it doable with the K8S included in Docker for Mac, or is another type of K8S deployment needed?
Gopher: Can I connect it with my local minikube?
Gopher: Is Docker running locally on Windows?
Florin: This will work with any Kubernetes deployment. In my presentation, I used Kubernetes on Windows.
The plugin will connect to any Kubernetes-compatible server, as long as the .kube/config configuration defaults to it and the API is compatible with the Kubernetes API.
For Docker on Mac there shouldn’t be any special requirements.

Gopher: How hard would it be to move this application to the GCP KE?
Florin: Not very hard. The only problem would be in how the database is initialized, as the file is loaded from the local machine. You’d need to create a container image with all the information needed or have the application create the initial database layout, or do it manually. Then you should be good to go.

Gopher: DB Question: Did you deploy Postgres on the localhost as a separate docker-container?
Florin: Yes, it was deployed using Kubernetes. See our blog post series on this.

Regarding the IDE configuration:

Gopher: Can I view mounted Config Maps in the editor?
Florin: Yes, it’s available under Configuration | Config Maps in the Kubernetes view of the Service Tool Window.

Gopher: Is there an integrated panel/interface for running tests on GoLand, or should I just open a terminal and `go test ./…` it?
Florin: Yes, you can run tests/benchmarks directly from the IDE. See the GoLand testing help documentation for more details about this.
You can create Run Configurations that are capable of using the same commands that you’d run in the (built-in) Terminal.

Gopher: Is there a way to disable those “parameter helpers” (by that I mean for instance when I type os.Getenv(“X”), GoLand shows os.Getenv(key: “X”))? While I find it *amazing*, I’d like to be able to disable it when, for instance, taking a screenshot off my code so as to not confuse people not using GoLand…
Florin: Yes, they are called Inline Hints and they can be disabled using Alt+Enter or by right-clicking on them. Alternatively, you can use Settings/Preferences | Editor | Inlay Hints.

Gopher: Is there any way to delete the K8S deployment and all it includes from within the GoLand UI?
Florin: Not at the moment, watch this issue for updates.

Gopher: Will you also share your live templates?
Florin: I’m currently using a single live template in the presentation. You can find it in this repository. If you’d like to learn more, check out the GoLand Live Templates guide. You can also join our upcoming webinar on GoLand Tips & Tricks at the beginning of June, stay tuned for further details.

Gopher: Are there any ways to run tests automatically before committing to Github?
Florin: Not at the moment. We have an open issue about this but it’s not on the roadmap yet.

Gopher: Could you share the plugins you installed?
Florin: I used the following plugins during the presentation: Presentation Assistant, Rainbow Brackets, and Gopher.

I also highly recommend Key Promoter X if you are just getting started with the IDE or if you want to learn the shortcuts as you work.

Aside from those, I also have the following plugins installed: Grep Console, Ideolog, Properties, Task Management, Time Tracking, Sass, Protobuf Support, Makefile support, Node.js, and Toml.

Gopher: Which Editor Color Scheme are you using? Is it custom or built-in?
Florin: I use a default IDE installation and in this particular case I used Darcula with Rainbow Brackets and Gopher plugins, see the above question.

Gopher: I am interested in understanding the complete setup of the environment. Is there a tutorial?
Florin: The environment used was set up from scratch. We’ll hold a webinar at the end of June about setting up your work environment from scratch.

Gopher: Does GoLand support OpenAPI/Swagger? Can we do code generation on the IDE?
Florin: From 2020.2 it will.

In the following section, I’ll recommend only packages that I have personal experience with. That doesn’t mean they are the best in their domain, they just happened to fill the needs I had at the time when I used them.

Gopher: Are there any ORM libraries for Go similar to Hibernate in Java?
Florin: There are different ORMs in Go. Off the top of my head, I can suggest you look at https://gorm.io/ or https://github.com/gobuffalo/pop, or https://github.com/volatiletech/sqlboiler. There are more you can browse through here https://github.com/avelino/awesome-go#orm.
I have used sqlx in all of my work so far.

Gopher: For web development, would you recommend going with plain vanilla Go or use a framework like Echo? Also, can you recommend any good logging libraries?
Gopher: What is the recommended framework to use in production for building microservices? like Spring for Java.
Gopher: Based on your experience, what is the best microservice framework, GoKit, or Micro?
Florin: I started with net/http back in the day and then added gorilla/mux on top of it. Most of the projects I wrote use either of these two. That being said, there are a lot of frameworks that you can try. Off the top of my head, I can suggest gin, echo, buffalo, or beego, but you can find a lot more here: https://github.com/avelino/awesome-go#web-frameworks.

Regarding logging, I generally used either the standard library log package or logrus. Some people also prefer zap, and there are a lot of other options here: https://github.com/avelino/awesome-go#logging.

I don’t have any particular opinion on either GoKit or Micro, as they each solve problems in their own very specific way.

Gopher: Do you recommend having a type for response data and a type for the database model?
Florin: Yes, that would make sense. During the presentation, I wanted to show how a JSON string can be quickly written using the IDE support, but in a production environment, I’d probably use a typed JSON response most of the time.

Gopher: What would you use for securing your handler? TLS module or just istio/linkerd?
Florin: TLS handler, but istio/linkerd are not bad options either.

Gopher: Is the ‘HTTP file’ thing unique to GoLand / Go?
Florin: You can use the HTTP client in all JetBrains IDEs, see the GoLand in-editor HTTP client documentation for more information about this feature.

Gopher: Can I code Kubernetes operators in a similar way?
Florin: Yes, you can code any kind of application using the same workflow shown during the webinar.

Gopher: Can I both debug and hot reload the code in a Docker container by installing delve in the container?
Florin: You can debug an application that runs inside a container, as in the webinar.
As for the hot reloading of code, that’s not a trivial task to do, especially in different operating systems. I may publish a blog post about this in the future if there is enough interest in it.

Gopher: Are you able to do remote debugging in remote Kubernetes environments within GoLand?
Florin: Yes, that would not be a problem. Follow the same steps as in the webinar.

Gopher: Hi, let’s say you want to divide a project into 2 microservices. The 1st microservice is responsible for sign up and log in. The 2nd Microservice is responsible for interacting with the database. How will these two microservices communicate? Will I have to use 2 different project folders with each project having its own two Go module files?
Florin: You can expose an API for the two different services to communicate with each other. It can be as simple as using a standard JSON API over HTTP, or you can use something more advanced like Protobuf and gRPC.

There is no easy way to tell what’s better, as it depends on a lot of variables. But if you are a small team, or alone, I’d recommend using a single repository with all the sources in it, in a single project that uses the pattern of multiple cmd folders to break down the application into different components. You can read about this in this article from Dave Cheney, specifically the Multiple commands and multiple packages section.

Gopher: Is there a gRPC equivalent of the http request file?
Florin: Not at the moment, but I created this request for it. Please watch it/vote for it to receive updates.

Gopher: Is there a profiling plugin for mutex contention & memory/CPU in the IDE?
Florin: There is no specific plugin for this as the functionality is built-in. To run these profilers, you have to write a test or a benchmark. See our help documentation on this.

Gopher: How about the CGO file, can GoLand handle it as easily as Go libraries?
Florin: At the moment GoLand does not support editing C files with the same level of features as it does for Go. Basic highlighting is supported for C, and you can reference C types once they are imported in Go. If you need to work on C sources, then please use CLion, our dedicated C IDE. Please note that it does not support Go editing at the moment either.
However, we plan to improve the editing experience for some use-cases, watch/vote for this issue to receive updates when we’ll have any.

Gopher: If you are working with multiple databases, can you specify what database you want completion for in your Go files?
Florin: You can choose to select which database to use using the database name prefix in the queries. E.g. for db1 and db2, you can refer to queries like db1.table.column and db2.table.column.

Gopher: Is there any good way to work with Helm via GoLand?
Florin: Yes, Helm support is present once you install the Kubernetes plugin. See our help documentation about this.

This is all for now. I hope they addressed all the other questions that I couldn’t answer during the webinar.

There are a few other questions that I could not address here as they require a bit more details from the person that asked them. I’ll reach out to you in the coming days to help you out and address them as well.

Thank you once again for your participation and see you at our next event. Until then, please let us know your thoughts in the comments section below, on our issue tracker, or tweet to us @GoLandIDE, or to me @dlsniper.

image description