Data Types in Go Last Updated : 23 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointers, slices, maps, functions, and channels come under this category.Interface type Here, we will discuss Basic Data Types in the Go language. The Basic Data Types are further categorized into three subcategories which are: NumbersBooleansStringsNumbers In Go language, numbers are divided into three sub-categories that are: Integers: In Go language, both signed and unsigned integers are available in four different sizes as shown in the below table. The signed int is represented by int and the unsigned integer is represented by uint.Possible arithmetic operations : Addition, subtraction, multiplication, division, remainderData Type Description int88-bit signed integerint1616-bit signed integerint3232-bit signed integerint6464-bit signed integeruint88-bit unsigned integeruint1616-bit unsigned integeruint3232-bit unsigned integeruint6464-bit unsigned integerintBoth int and uint contain same size, either 32 or 64 bit.uintBoth int and uint contain same size, either 32 or 64 bit.runeIt is a synonym of int32 and also represent Unicode code points.byteIt is a synonym of uint8.uintptrIt is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value. Example: Go // Go program to illustrate // the use of integers package main import "fmt" func main() { // Using 8-bit unsigned int var X uint8 = 225 fmt.Println(X, X-3) // Using 16-bit signed int var Y int16 = 32767 fmt.Println(Y+2, Y-2) } Output: 225 222 -32767 32765 Example of arithmetic operations : Go // Possible arithmetic operations for intigers // Author : Chhanda Saha package main import "fmt" func main() { var x int16 = 170 var y int16 = 83 //Addition fmt.Printf(" addition : %d + %d = %d\n ", x, y, x+y) //Subtraction fmt.Printf("subtraction : %d - %d = %d\n", x, y, x-y) //Multiplication fmt.Printf(" multiplication : %d * %d = %d\n", x, y, x*y) //Division fmt.Printf(" division : %d / %d = %d\n", x, y, x/y) //Modulus fmt.Printf(" remainder : %d %% %d = %d\n", x, y, x%y) } Output: addition : 170 + 83 = 253 subtraction : 170 - 83 = 87 multiplication : 170 * 83 = 14110 division : 170 / 83 = 2 remainder : 170 % 83 = 4 Floating-Point Numbers: In Go language, floating-point numbers are divided into two categories as shown in the below table.Possible arithmetic operations : Addition, subtraction, multiplication, division.Three literal styles are available :decimal (3.15)exponential ( 12e18 or 3E10)mixed (13.16e12)Data TypeDescription float3232-bit IEEE 754 floating-point numberfloat6464-bit IEEE 754 floating-point number Example: Go // Go program to illustrate // the use of floating-point // numbers package main import "fmt" func main() { a := 20.45 b := 34.89 // Subtraction of two // floating-point number c := b-a // Display the result fmt.Printf("Result is: %f", c) // Display the type of c variable fmt.Printf("\nThe type of c is : %T", c) } Output: Result is: 14.440000 The type of c is : float64 Example of arithmetic operations for floating point numbers : Go // Possible arithmetic operations for float numbers // Author : Chhanda Saha package main import "fmt" func main() { var x float32 = 5.00 var y float32 = 2.25 //Addition fmt.Printf("addition : %g + %g = %g\n ", x, y, x+y) //Subtraction fmt.Printf("subtraction : %g - %g = %g\n", x, y, x-y) //Multiplication fmt.Printf("multiplication : %g * %g = %g\n", x, y, x*y) //Division fmt.Printf("division : %g / %g = %g\n", x, y, x/y) } Output: addition : 5 + 2.25 = 7.25 subtraction : 5 - 2.25 = 2.75 multiplication : 5 * 2.25 = 11.25 division : 5 / 2.25 = 2.2222223Complex Numbers: The complex numbers are divided into two parts are shown in the below table. float32 and float64 are also part of these complex numbers. The in-built function creates a complex number from its imaginary and real part and in-built imaginary and real function extract those parts.There are few built-in functions in complex numbers:complex - make complex numbers from two floats.real() - get real part of the input complex number as a float number.imag() - get imaginary of the input complex number part as float numberData TypeDescription complex64Complex numbers which contain float32 as a real and imaginary component.complex128Complex numbers which contain float64 as a real and imaginary component. Example: Go // Go program to illustrate // the use of complex numbers package main import "fmt" func main() { var a complex128 = complex(6, 2) var b complex64 = complex(9, 2) fmt.Println(a) fmt.Println(b) // Display the type fmt.Printf("The type of a is %T and "+ "the type of b is %T", a, b) } Output: (6+2i) (9+2i) The type of a is complex128 and the type of b is complex64 Built-in functions example : Go // Built-in functions in complex numbers // Author : Chhanda Saha package main import "fmt" func main() { comp1 := complex(10, 11) // complex number init syntax comp2 := 13 + 33i fmt.Println("Complex number 1 is :", comp1) fmt.Println("Complex number 1 is :", comp2) // get real part realNum := real(comp1) fmt.Println("Real part of complex number 1:", realNum) // get imaginary part imaginary := imag(comp2) fmt.Println("Imaginary part of complex number 2:", imaginary) } Output: Complex number 1 is : (10+11i) Complex number 1 is : (13+33i) Real part of complex number 1: 10 Imaginary part of complex number 2: 33Booleans The boolean data type represents only one bit of information either true or false. The values of type boolean are not converted implicitly or explicitly to any other type. Example: Go // Go program to illustrate // the use of booleans package main import "fmt" func main() { // variables str1 := "GeeksforGeeks" str2:= "geeksForgeeks" str3:= "GeeksforGeeks" result1:= str1 == str2 result2:= str1 == str3 // Display the result fmt.Println( result1) fmt.Println( result2) // Display the type of // result1 and result2 fmt.Printf("The type of result1 is %T and "+ "the type of result2 is %T", result1, result2) } Output: false true The type of result1 is bool and the type of result2 is boolStrings The string data type represents a sequence of Unicode code points. Or in other words, we can say a string is a sequence of immutable bytes, means once a string is created you cannot change that string. A string may contain arbitrary data, including bytes with zero value in the human-readable form. Strings can be concatenated using plus(+) operator. Example: Go // Go program to illustrate // the use of strings package main import "fmt" func main() { // str variable which stores strings str := "GeeksforGeeks" // Display the length of the string fmt.Printf("Length of the string is:%d", len(str)) // Display the string fmt.Printf("\nString is: %s", str) // Display the type of str variable fmt.Printf("\nType of str is: %T", str) } Output: Length of the string is:13 String is: GeeksforGeeks Type of str is: string String concatenation example: Go // String concatenation // Author : Chhanda Saha package main import "fmt" func main() { var str1 string = "STRING_" var str2 string = "Concatenation" // Concatenating strings using + operator fmt.Println("New string : ", str1+str2) } Output: New string : STRING_Concatenation Comment More infoAdvertise with us Next Article Go Variables A ankita_saini Follow Improve Article Tags : Go Language Go-Basics Golang Similar Reads Go Tutorial Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google 2 min read OverviewGo Programming Language (Introduction)Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env 11 min read How to Install Go on Windows?Prerequisite: Introduction to Go Programming Language Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robe 3 min read How to Install Golang on MacOS?Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but 4 min read Hello World in GolangHello, World! is the first basic program in any programming language. Letâs write the first program in the Go Language using the following steps:First of all open Go compiler. In Go language, the program is saved with .go extension and it is a UTF-8 text file.Now, first add the package main in your 3 min read FundamentalsIdentifiers in Go LanguageIn programming languages, identifiers are used for identification purposes. In other words, identifiers are the user-defined names of the program components. In the Go language, an identifier can be a variable name, function name, constant, statement label, package name, or type. Example: package ma 3 min read Go KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as an identifier. Doing this will result in a compile-time error. Example: C // Go program to illustrate the // use of key 2 min read Data Types in GoData types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointer 7 min read Go VariablesA typical program uses various values that may change during its execution. For Example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another 9 min read Constants- Go LanguageAs the name CONSTANTS suggests, it means fixed. In programming languages also it is same i.e., once the value of constant is defined, it cannot be modified further. There can be any basic data type of constants like an integer constant, a floating constant, a character constant, or a string literal. 6 min read Go OperatorsOperators are the foundation of any programming language. Thus the functionality of the Go language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In the Go language, operators Can be categorized based on their different functiona 9 min read Control StatementsGo Decision Making (if, if-else, Nested-if, if-else-if)Decision making in programming is similar to decision making in real life. In decision making, a piece of code is executed when the given condition is fulfilled. Sometimes these are also termed as the Control flow statements. Golang uses control statements to control the flow of execution of the pro 5 min read Loops in Go LanguageGo language contains only a single loop that is for-loop. A for loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. In Go language, this for loop can be used in the different forms and the forms are: 1. As simple for loop It is similar t 5 min read Switch Statement in GoIn Go, a switch statement is a multiway branch statement that efficiently directs execution based on the value (or type) of an expression. There are two main types of switch statements in Go:Expression SwitchType SwitchExamplepackage mainimport "fmt"func main() { day := 4 switch day { case 1: fmt.Pr 2 min read Functions & MethodsFunctions in Go LanguageIn Go, functions are blocks of code that perform specific tasks, which can be reused throughout the program to save memory, improve readability, and save time. Functions may or may not return a value to the caller.Example:Gopackage main import "fmt" // multiply() multiplies two integers and returns 3 min read Variadic Functions in GoVariadic functions in Go allow you to pass a variable number of arguments to a function. This feature is useful when you donât know beforehand how many arguments you will pass. A variadic function accepts multiple arguments of the same type and can be called with any number of arguments, including n 3 min read Anonymous function in Go LanguageAn anonymous function is a function that doesnât have a name. It is useful when you want to create an inline function. In Go, an anonymous function can also form a closure. An anonymous function is also known as a function literal.ExampleGopackage main import "fmt" func main() { // Anonymous functio 2 min read main and init function in GolangThe Go language reserve two functions for special purpose and the functions are main() and init() function.main() functionIn Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. The main() function is a spec 2 min read What is Blank Identifier(underscore) in Golang?_(underscore) in Golang is known as the Blank Identifier. Identifiers are the user-defined name of the program components used for the identification purpose. Golang has a special feature to define and use the unused variable using Blank Identifier. Unused variables are those variables that are defi 3 min read Defer Keyword in GolangIn Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns. In other words, defer function or method call arguments evaluate instantly, but they don't execute until the nearby functions returns. You can create a deferred m 3 min read Methods in GolangGo methods are like functions but with a key difference: they have a receiver argument, which allows access to the receiver's properties. The receiver can be a struct or non-struct type, but both must be in the same package. Methods cannot be created for types defined in other packages, including bu 3 min read StructureStructures in GolangA structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-orient 7 min read Nested Structure in GolangA structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. Go language allows nested structure. A structure which is the 3 min read Anonymous Structure and Field in GolangIn Golang, structures (or structs) allow us to group elements of various types into a single unit, which is useful for modeling real-world entities. Anonymous structures are unnamed, temporary structures used for a one-time purpose, while anonymous fields allow embedding fields without names.Example 3 min read ArraysArrays in GoArrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequen 7 min read How to Copy an Array into Another Array in Golang?In Go, an array is a fixed-length sequence that holds elements of a specific type. Unlike slices, arrays have a constant size, which is determined when the array is declared. Copying one array to another is straightforward but requires that both arrays have the same length and type.Examplepackage ma 3 min read How to pass an Array to a Function in Golang?In Go, arrays are used to store a fixed-length collection of data of the same type. To manage this data effectively, you may often need to pass arrays to functions. In this article we will learn "How to pass an Array to a Function in Golang".Example:Gopackage main import "fmt" // Function to calcula 2 min read SlicesSlices in GolangSlices in Go are a flexible and efficient way to represent arrays, and they are often used in place of arrays because of their dynamic size and added features. A slice is a reference to a portion of an array. It's a data structure that describes a portion of an array by specifying the starting index 14 min read Slice Composite Literal in GoThere are two terms i.e. Slice and Composite Literal. Slice is a composite data type similar to an array which is used to hold the elements of the same data type. The main difference between array and slice is that slice can vary in size dynamically but not an array. Composite literals are used to c 3 min read How to sort a slice of ints in Golang?In Go, slices provide a flexible way to manage sequences of elements. To sort a slice of ints, the sort package offers a few straightforward functions. In this article we will learn How to Sort a Slice of Ints in Golang.ExampleGopackage main import ( "fmt" "sort" ) func main() { intSlice := []int{42 2 min read How to trim a slice of bytes in Golang?In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you ar 3 min read How to split a slice of bytes in Golang?In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt 3 min read StringsStrings in GolangIn the Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where every character is represented by one or more bytes using UTF-8 Encoding. In other words, strings are the immutable chain of arbitrary bytes(including bytes 7 min read How to Trim a String in Golang?In Go, strings are UTF-8 encoded sequences of variable-width characters, unlike some other languages like Java, python and C++. Go provides several functions within the strings package to trim characters from strings.In this article we will learn how to Trim a String in Golang.Examples := "@@Hello, 2 min read How to Split a String in Golang?In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the str 3 min read Different ways to compare Strings in GolangIn Go, strings are immutable sequences of bytes encoded in UTF-8. You can compare them using comparison operators or the strings.Compare function. In this article,we will learn different ways to compare Strings in Golang.Examplepackage main import ( "fmt" "strings" ) func main() { s1 := "Hello" s2 : 2 min read PointersPointers in GolangPointers in Go programming language or Golang is a variable that is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always f 8 min read Passing Pointers to a Function in GoPrerequisite: Pointers in Go Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. You can also pass the pointers to the function like the variables. There are two ways to do this as follows: Create a pointer and simply pass it to 3 min read Pointer to a Struct in GolangIn Go, structs are used to create custom data types that group different fields together. When working with structs, using pointers can be especially beneficial for managing memory efficiently and for avoiding unnecessary copying of data. A pointer to a struct allows you to directly reference and mo 3 min read Go Pointer to Pointer (Double Pointer)Prerequisite: Pointers in Go Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. A pointer is a special variable so it can point to a variable of any type even to a pointer. Basically, this looks like a chain of pointers. When we 4 min read Comparing Pointers in GolangPointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang are also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always 3 min read Like