How to learn Go
Learning Go (or Golang) is a great choice for programming. Here are some steps to get started:
- Install Go: First, download and install the Go programming language from the official website (golang.org). Follow the installation instructions for your specific operating system.
- Learn the Basics:
- Start with the official Go tour: Visit the “Tour of Go” at https://tour.golang.org/welcome/1 to get a hands-on introduction to the language.
- Learn the basics: Understand Go’s syntax, data types, variables, and control structures. There are many online tutorials and resources available.
- IDE/Editor: Choose an integrated development environment (IDE) or code editor for Go. Popular choices include Visual Studio Code with Go extensions, GoLand, or even simple text editors like VSCode or Sublime Text.
- Write Code: Begin writing simple Go programs to practice what you’ve learned. Start with small projects and gradually work your way up to more complex tasks.
- Use the Standard Library: Go has a powerful standard library. Explore and use it for various tasks like file handling, networking, and more.
- Concurrency: One of Go’s strengths is its concurrency support. Learn about goroutines and channels, as they are fundamental to building concurrent applications.
- Read Books and Documentation:
- “The Go Programming Language” by Alan A. A. Donovan and Brian W. Kernighan is a highly recommended book.
- Explore the official Go documentation at https://golang.org/doc/ to deepen your understanding.
- Online Resources: Take advantage of online resources such as blogs, forums, and Stack Overflow to seek help, ask questions, and learn from the Go community.
- Build Projects: As you gain confidence, start building your own projects. This practical experience is invaluable for mastering Go.
- Contribute to Open Source: Consider contributing to open-source Go projects. It’s a great way to learn from experienced developers and give back to the community.
- Keep Up-to-Date: The Go ecosystem evolves. Stay updated with new features and best practices.
- Practice, Practice, Practice: Like any programming language, practice is key to becoming proficient. Keep coding and solving real-world problems.
Learning Go can be a rewarding experience, especially if you’re interested in systems programming, cloud services, or building high-performance applications. Good luck!
Learning Go for system programming
Learning Go (Golang) for system programming is a great choice, as Go is known for its performance and ease of use. Here’s a step-by-step guide to get started:
- Install Go: Begin by installing Go on your system. You can download the official installer from the Go website and follow the installation instructions for your platform.
- Learn the Basics:
- Start with the official Go Tour at tour.golang.org. It covers the fundamentals and syntax of Go.
- Read the official Go documentation at golang.org/doc. It’s a comprehensive resource for understanding the language.
- Understand Concurrency:
- Go is well-known for its concurrency support. Learn about Goroutines and Channels, which are essential for system programming tasks.
- Practice by creating small concurrent programs.
- Explore the Standard Library:
- Go has a powerful standard library that’s particularly useful for system programming. Familiarize yourself with packages like
os,syscall, andunsafe.
- Go has a powerful standard library that’s particularly useful for system programming. Familiarize yourself with packages like
- Learn About Memory Management:
- Understand how Go manages memory, including garbage collection and manual memory management techniques like using pointers.
- File I/O and Networking:
- Explore how to perform file I/O and network operations. The
osandnetpackages will be your friends here.
- Explore how to perform file I/O and network operations. The
- Error Handling:
- Go uses a unique approach to error handling. Learn how to work with errors effectively using the
errortype and thepanicandrecovermechanisms.
- Go uses a unique approach to error handling. Learn how to work with errors effectively using the
- Build Small Projects:
- Start with small projects to practice what you’ve learned. For example, create a simple file system utility or a network tool.
- Learn C Interoperability:
- If you’re doing low-level system programming, learn how to interact with C code using cgo.
- Explore System Calls:
- System programming often involves making system calls. Learn about the
syscallpackage for making these calls.
- System programming often involves making system calls. Learn about the
- Read Books and Tutorials:
- Consider reading books like “The Go Programming Language” by Alan A. A. Donovan and Brian W. Kernighan. Also, explore online tutorials and blogs.
- Practice and Experiment:
- The best way to learn system programming is through practical experience. Experiment with creating your own utilities or system tools.
- Participate in the Go Community:
- Join forums, mailing lists, or online communities where you can ask questions, share your knowledge, and learn from others.
- Stay Updated:
- Go is an evolving language. Stay updated with the latest Go releases and best practices.
Remember that system programming can be complex and requires careful attention to detail. Start small, build your knowledge gradually, and don’t hesitate to seek help from the Go community when needed.
Use cases for Go
Go (often referred to as Golang) is a versatile programming language with a range of uses across different domains. Some of the primary use cases for Go include:
- System Programming: Go is well-suited for system-level programming and building low-level components. It offers good support for concurrency and is used in building network servers, operating systems, and device drivers.
- Web Development: Go has a strong standard library for building web applications. Frameworks like Gin, Echo, and Beego make it easy to develop web services and RESTful APIs. It’s also used for developing serverless functions.
- Cloud Computing: Many cloud providers, such as Google Cloud, AWS, and Azure, have official SDKs or third-party libraries for Go. This makes Go a popular choice for building cloud-native applications and working with cloud services.
- Containerization: Go is the language behind Docker and Kubernetes, two of the most popular containerization and orchestration technologies. It’s used for building and managing containerized applications.
- Networking: Go’s built-in support for concurrency and its net package make it well-suited for network programming. It’s commonly used in developing network servers, proxies, and load balancers.
- Database Systems: Go has a variety of database drivers and libraries for connecting to and working with different database systems. This includes relational databases like PostgreSQL and NoSQL databases like MongoDB.
- DevOps and Automation: Go is a popular choice for building command-line tools, scripts, and automation for DevOps tasks. Its simplicity, performance, and the availability of third-party libraries make it a good fit for this purpose.
- IoT (Internet of Things): Go is used in building software for IoT devices, particularly when they require efficient, low-level control and networking capabilities.
- Game Development: Although not as popular as languages like C++ or Unity for game development, Go has libraries and frameworks like Engo that can be used for creating 2D games.
- Scientific and Numerical Computing: While Go is not the first choice for scientific computing, it has libraries like gonum for numerical and scientific computations.
- Security and Cryptography: Go has libraries for encryption, decryption, and secure communication, making it suitable for building secure applications.
- Machine Learning: Although not as prevalent as languages like Python, Go has machine learning libraries and frameworks, such as Gorgonia and Golearn, that can be used for developing ML applications.
- Embedded Systems: Go can be used in embedded systems development, especially when performance and efficiency are crucial.
- Financial and Trading Systems: Go’s strong concurrency support and performance characteristics make it suitable for building financial and trading applications.
These are just a few examples of the diverse range of applications for which Go is well-suited. Go’s simplicity, efficiency, and strong support for concurrency make it an attractive choice for a wide variety of programming tasks.
Golang Pros
- Modular design means the compiled binary is as small as possible and requires no dependencies.
- Like Java, it’s platform independent. You can compile it for any platform your servers and applications run on. Take that, Apple.
- Easy for your developers to learn because it’s based on C. You don’t need to go to a code bootcamp to become proficient.
- Has static type checking, meaning your code gets its bugs cleaned up at compile time, not in production.
- Doesn’t require an IDE, but still supports them, so you can choose your own weapon or go au naturale if that’s your style.
Golang Cons
- Libraries are not established. Compared to Java, Go seems like it doesn’t even have libraries. True, golang has plenty of standard packages in its library, and a few more community-driven sub-repositories, but don’t expect to find “Top 100” lists of golang libraries like you do with Java, for instance. You might need to create your own library or get creative if your project needs one that isn’t available.
- Conventions are specified and enforced through the compiler and formatter but are significantly different from other languages.
- For example, exported (“public”) variables and functions must be capitalized. While it reduces boilerplate, it creates a semantical difference between variable names that isn’t obvious unless you know about it.
- Switch statement cases don’t require the
breakkeyword; instead, if you want the execution to continue to the next case, you must insert thefallthroughkeyword. - All the files in a single directory must have a single package. There is no getting around this, even if you want a ‘main’ package that calls into your ‘app’ package for multiple entry points or binaries.
- Doesn’t have generics. Most languages utilize them (all the languages we mentioned at the beginning do), and generics really help with making your code reusable for different applications. But why are they needed? Remember how we pointed out that Go is fast, like ludicrously fast? One of the reasons is because it doesn’t implement generics, so compile time and runtime are sped up. If you need to problem-solve with golang and still need generic functionality, try an array, slice, or map instead.
What’s Go Good At?
Network and Web Server Applications
Infoworld says that Go’s native concurrency features make it shine because of its goroutines (lightweight threads) and channels. This is why we are rewriting Lumberjack’s smart agent in golang (more on that in the near future).
It’s also why container giants Docker and Kubernetes are built on golang. It’s lightning fast, and it’s perfect for serving up your applications. In fact, it’s so efficient that Netflix moved its network infrastructure from the once-invincible Node.js to Go. And as Uber was having difficulty managing geofence requests for their platform, they moved from Node.js to golang as well. Here’s what Uber’s engineering manager Kai Wei had to report on his company’s transition in 2015:
“This service has had 99.99% uptime since inception. The only downtime was caused by beginner programming errors and a file descriptor leak bug in a third party library. Importantly, we haven’t seen any issues with Go’s runtime.”
Go is fast. Really fast.
According to the Computer Language Benchmarks Game hosted on Debian.org, golang is fast. Like 100x or more fast. Golang beats Python in task performance on everything from CPU load, memory usage and more. Reason being is because it’s a low-level language, focused on fast compiling and faster runtimes. This is why file-sharing heavyweight Dropbox moved their sync platform from Python to Go in 2014 — and they haven’t looked back. Video game broadcasting service Twitch had a problem with garbage collection and also made the switch to golang in 2015. After implementing, they reported “a 20x improvement in garbage collection (GC) pause time” and “another 10x improvement in Go 1.6’s pause.” They also reported an additional 10x speedup in Go 1.7, which obsoleted their manual tuning. (Golang is currently on version 1.8, released in February 2017.)
When we started porting Lumberjack’s agent into golang from its previous Python iteration, we noticed speed improvements of up to 100x over its older version. We’re excited to pass that on to customers after our beta ends in the summer.
What’s Go Not So Good At?
So far we’ve been singing praises in Google’s general direction. But Golang can’t be and is not the One Language to rule them all. Where does golang fall short and what should DevOps people be wary of when adopting?
Desktop Uses/Apps with GUI
Golang wasn’t meant to be a great language for GUI applications. Sure, a library once existed for that on GitHub, but it has been abandoned. If you’re needing a GUI with your Go application, trying pairing it with an HTML5 web interface instead.
Generics? What Generics?
As mentioned above, golang doesn’t have compile-time generics, which means you might be required to have duplicate code or type-cast to accomplish your purposes. Language extensibility can make some tasks more verbose as well, meaning more code. However, given that golang is an extremely lightweight language already, you might find that without generics, your codebase ends up being significantly smaller than on other languages anyways.
System-level Programming
Think kernels, drivers, and embedded systems. This is what Mozilla’s Rust was built for. And this is also why Google’s own operating systems, Chrome OS and Android, aren’t built on Go.
Where Can I Learn Golang for DevOps Applications?
https://github.com/avelino/awesome-go#devops-tools
So you’ve bit the bullet and want to start learning Go. Where do you, um, go for that? Given the open-source nature of the language, there’s a ton of resources available from the community, almost all of them free.
First, you can get started by going to golang’s own introduction at tour.golang.org. The tour is meant for programmers; if this is your first language, don’t start there.
For community support, there’s the Golang-dev development list. Meanwhile, the Golang-nuts Google Group is quite active and is staffed by a cadre of passionate golang fans. The principal authors of golang, Robert Griesemer, Rob Pike, and Ken Thompson, have also been known to speak up on the forum from time to time.
Additionally, Karl Seguin’s popular 84-page “Little Go Book” is free and is available on the go (no pun intended) as a PDF. A GitHub wiki lists some of the more popular online resources for learning golang as well. Finally, online learning platform Udemy has a number of Go courses ranging from $20 to $200.
How can I clean up my Golang code?
We recommend starting with gofmt first, a built-in library that will clean up your project code. While gofmt is great for formatting your code, it doesn’t make it pretty. For that, you can use golint, a community-driven linting library that is maintained on GitHub. By the way, if you’re interested in error checking, check out go vet, which vets code for errors not caught by compilers, such as Printf arguments that don’t match the format string.
Ready, Set, Golang
We hope this miniguide has been a useful primer on golang and why we think it’s the perfect language for your DevOps projects. We think Golang is so good that we are porting Lumberjack’s agent from Python over to it, and we’re already noticing major improvements our customers will love. Check back on the Blue Matador blog in the near future to read why Lumberjack is better in Go.