9.5 C
New York
Sunday, November 27, 2022

What Golang generics support means for code structure – TechTarget

Getty Images/iStockphoto
Go, also known as Golang, is a compiled programming language that is considered strongly and statically typed. The language added generics support in March 2022, an update that has the potential to simplify a project’s overall codebase.
But before diving into Golang generics, programmers must understand Go’s type system and how it fits into the overall programming language universe.
Strongly typed programming languages only allow operations on certain variable types. For example, a strongly typed language would not allow an integer to be added to a string because the language deems the two data types incompatible.
A weakly typed language, in contrast, would automatically convert one of the variables to the other type and perform the addition.
As an example, consider the following line of Go code.
Because the types are mismatched — “1” is a string and 1 is an integer — the addition operation cannot be performed. Instead, the compiler returns the following error.
Compare this output with the results of the same operation in JavaScript, a weakly typed language.
This returns the string “11” because JavaScript converts the integer to a string to concatenate the two values. JavaScript does not hold a strong definition of provided data types and instead casts them implicitly.
The question of whether a language is strongly or weakly typed isn’t always straightforward. No language is 100% strongly or weakly typed, as features can be individually strongly or weakly typed.
For the most part, Go is described as strongly typed for the reason shown above. However, there are examples of weakly typed features in Go. Specifically, Go interfaces are implemented implicitly, without any implements keyword. If a type implements methods defined by an interface, then it implements that interface automatically.
Static and dynamic typing describe whether a programming language checks a variable’s type at compile time or runtime. A common misconception is that all strongly typed languages are statically typed and, likewise, all weakly typed languages are dynamically typed.
Go is statically typed and therefore cannot update a variable’s type at runtime. Consider the following example.
Upon running this code, Go’s compiler fails and returns the following error.
Because the variable s is statically typed as a string, it can only be updated with string values.
The following is an example of type reassignment in Python, a dynamically typed language where these lines cause no issue in execution.
From a general programming perspective, generics are functions or data structures that can be written without defining specific types. With generics, it’s possible to write a function that accepts a generic type and can be called on many different types.
As an example, imagine writing a function to reverse a list of integers. In a language without generics, a programmer would need to write a separate function for each type of list. The program would require separate reverse functions to handle a list of integers, a list of strings and a list of lists.
The goal of generics is to reduce duplicated code that arises when the same functions are performed on different types. Instead of defining separate reverse functions for lists of integers and lists of strings, generics allow programmers to define a function for a generic type, which can then be called on a list made up of values of any type.
For most of Go’s history, the language did not support generics. Go’s creators and lead engineers were initially concerned that the addition of generics would create unnecessary complexity or lead to slower programs. Generics, an influential addition to the language, were introduced in March 2022.
Below is an example of present-day Go generics in action.
The generic function’s signature includes the any keyword, which specifies that T — our generic type — can be any type. To specify that the function can only be used with certain types, such as strings and integers, write those types in place of the any keyword. For a function that sorts a list, using the Ordered keyword restricts the function to types that can be compared using the >, >=, < and <= operators.
Follow a Kubernetes and Go tutorial in your home lab
For greater API security and clearer boundaries for developers, experts at API World called for developer-focused security tools …
At this week’s API World, experts highlight issues with poor API documentation and suggest ways to fix it, such as ditching …
Java 20 will likely include useful features such as improvements to concurrency and profiling, but the incremental updates are …
Developers face numerous struggles trying to perform traditional, end-to-end integration testing on microservices. Contract …
More than ever, increases in data-centric developer reliance, data sources and users push developers to understand IT purchasing …
As with any software development cycle, API security must be built in from the start. Follow these guidelines to design, deploy …
AWS Batch enables developers to run thousands of batches within AWS. Follow this tutorial to set up this service, create your own…
Alloy, a new infrastructure platform, lets partners and Oracle-affiliated enterprises resell OCI to customers in regulated …
Dell dropped news at separate events this week — one that showcased edge management software, another that showed deepening HCI …
Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates …
There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service …
AWS users face a choice when deploying Kubernetes: run it themselves on EC2 or let Amazon do the heavy lifting with EKS. See …
Do you know Java? Are you trying to learn TypeScript? Here are five differences between TypeScript and Java that will make the …
The job a product manager does for a company is quite different from the role of product owner on a Scrum team. Learn key …
Want to prove your knowledge of Scrum? Tackle this 10-question Scrum introduction quiz and see how well you know the Scrum …
Data center network optimization can improve business impact and promote long-term equipment health. Look to pilot new equipment,…
Airflow in data centers is crucial for equipment health. While the hot aisle/cold aisle is popular, examine other options like …
File server reporting within File Server Resource Manager can help admins identify problems and then troubleshoot Windows servers…
All Rights Reserved, Copyright 2016 – 2022, TechTarget

Privacy Policy
Cookie Preferences
Do Not Sell My Personal Info

source

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles