search
HomeBackend DevelopmentGolangGo language data types revealed: fully displayed

Go language data types revealed: fully displayed

Jan 09, 2024 pm 02:29 PM
go languageDecrypttype of data

Go language data types revealed: fully displayed

Decrypting the data types of Go language: a clear overview, specific code examples are needed

1. Introduction
Data types in programming languages ​​​​are for developers Very importantly, it determines what kinds of data we can store and manipulate. In Go language, the concept of data types is similar to other programming languages, but Go language has its own unique data type system and characteristics. This article will give you an in-depth understanding of the data types of the Go language and decipher this family through specific code examples.

2. Basic data types

The basic data types of Go language include: Boolean, integer, floating point, complex number, string and character. They are bool, int, float, complex, string and rune. Let's take a look at them separately.

  1. Boolean type (bool)
    The Boolean type has only two values, namely true and false. Like other languages, we can use Boolean types to judge and control the flow of the program.

Sample code:

package main

import "fmt"

func main() {
   var b bool = true
   fmt.Println(b)
}
  1. Integer type (int)
    The integer data type can store integer values, and its size is related to the underlying computer architecture. In Go language, integers are divided into signed integers and unsigned integers. The following are some commonly used integer data types:
  • int8: signed 8-bit integer, ranging from -128 to 127.
  • int16: signed 16-bit integer, the value range is -32768 to 32767.
  • int32: signed 32-bit integer, the value range is -2147483648 to 2147483647.
  • int64: signed 64-bit integer, the value range is -9223372036854775808 to 9223372036854775807.
  • uint8: Unsigned 8-bit integer, value range is 0 to 255.
  • uint16: Unsigned 16-bit integer, value range is 0 to 65535.
  • uint32: Unsigned 32-bit integer, the value range is 0 to 4294967295.
  • uint64: Unsigned 64-bit integer, the value range is 0 to 18446744073709551615.
  • int: Depending on the number of bits in the operating system and compiler, 32-bit operating systems are 32-bit integers, and 64-bit operating systems are 64-bit integers.
  • uint: Depending on the number of bits in the operating system and compiler, 32-bit operating systems are 32-bit unsigned integers, and 64-bit operating systems are 64-bit unsigned integers.

Sample code:

package main

import "fmt"

func main() {
   var i int = 10
   fmt.Println(i)
}
  1. Floating point type (float)
    The floating point data type can store decimal values ​​and has two precisions: float32 and float64 .
  • float32: Single-precision floating point number, accurate to 7 decimal places.
  • float64: Double-precision floating point number, accurate to 15 digits after the decimal point.

Sample code:

package main

import "fmt"

func main() {
   var f float32 = 3.14
   fmt.Println(f)
}
  1. Complex number (complex)
    The complex number type is used to represent complex numbers whose real and imaginary parts are both floating point types. In Go language, complex types have two precisions: complex64 and complex128.
  • complex64: The real part and the imaginary part are represented by two float32.
  • complex128: The real part and the imaginary part are represented by two float64.

Sample code:

package main

import "fmt"

func main() {
   var c complex64 = complex(1, 2)
   fmt.Println(c)
}
  1. String (string)
    String is a data type in Go language, which is used to represent a string of characters. Strings are immutable, that is, they cannot be modified in the original string, but we can process strings through some functions and operations. In the Go language, strings are enclosed in double quotes (").

Sample code:

package main

import "fmt"

func main() {
   var s string = "Hello, World!"
   fmt.Println(s)
}
  1. Characters (rune)
    Characters (rune ) is the data type used to represent Unicode characters in the Go language. It is actually an integer type that represents the code point of the Unicode character. Characters are represented by single quotes (').

Sample code:

package main

import "fmt"

func main() {
   var r rune = '你'
   fmt.Println(r)
}

3. Composite data types

In addition to basic data types, Go language also provides some composite data types, including arrays, slices, dictionaries, and structures. Structure (struct), interface (interface) and function (function).

  1. Array (array)
    An array is a fixed-size data structure that can store a series of elements of the same type .In the Go language, the size of the array is fixed and cannot be changed.

Sample code:

package main

import "fmt"

func main() {
   var arr [3]int
   arr[0] = 1
   arr[1] = 2
   arr[2] = 3
   fmt.Println(arr)
}
  1. Slice
    Slice is a dynamic sized data structure, which can automatically expand as needed. Slices are created using the make function, or they can be created from arrays or other slices through the slice operator [:].

Sample code:

package main

import "fmt"

func main() {
   arr := []int{1, 2, 3}
   fmt.Println(arr)
}
  1. Dictionary (map)
    A dictionary is an unordered collection consisting of keys and values. In the Go language, a dictionary is created using the make function.

Sample code:

package main

import "fmt"

func main() {
   dict := make(map[string]int)
   dict["apple"] = 1
   dict["banana"] = 2
   dict["cherry"] = 3
   fmt.Println(dict)
}
  1. Structure (struct)
    Structure is a custom data type that can contain multiple fields. Each field has its own type and name. In the Go language, structures are defined using the type keyword.

Sample code:

package main

import "fmt"

type Person struct {
   Name string
   Age  int
}

func main() {
   p := Person{"Alice", 18}
   fmt.Println(p)
}
  1. Interface (interface)
    Interface is an abstract data type , which defines a set of methods. The interface can be implemented by a specific type, and the method that implements the interface can be called through the interface type.

Sample code:

package main

import "fmt"

type Shape interface {
   Area() float64
}

type Circle struct {
   Radius float64
}

func (c Circle) Area() float64 {
   return c.Radius * c.Radius * 3.14
}

func main() {
   var s Shape
   c := Circle{5}
   s = c
   fmt.Println(s.Area())
}
  1. 函数(function)
    函数是一段可重复使用的代码块,它可以接受参数并返回结果。在Go语言中,函数是一等公民,可以像其他值类型一样被传递和赋值。

示例代码:

package main

import "fmt"

func Add(a, b int) int {
   return a + b
}

func main() {
   sum := Add(1, 2)
   fmt.Println(sum)
}

总结
本文通过具体的代码示例对Go语言的数据类型进行了解密,详细介绍了Go语言的基本数据类型和复合数据类型。希望本文可以帮助大家更好地理解和应用Go语言的数据类型。

The above is the detailed content of Go language data types revealed: fully displayed. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
String Manipulation in Go: Mastering the 'strings' PackageString Manipulation in Go: Mastering the 'strings' PackageMay 14, 2025 am 12:19 AM

Mastering the strings package in Go language can improve text processing capabilities and development efficiency. 1) Use the Contains function to check substrings, 2) Use the Index function to find the substring position, 3) Join function efficiently splice string slices, 4) Replace function to replace substrings. Be careful to avoid common errors, such as not checking for empty strings and large string operation performance issues.

Go 'strings' package tips and tricksGo 'strings' package tips and tricksMay 14, 2025 am 12:18 AM

You should care about the strings package in Go because it simplifies string manipulation and makes the code clearer and more efficient. 1) Use strings.Join to efficiently splice strings; 2) Use strings.Fields to divide strings by blank characters; 3) Find substring positions through strings.Index and strings.LastIndex; 4) Use strings.ReplaceAll to replace strings; 5) Use strings.Builder to efficiently splice strings; 6) Always verify input to avoid unexpected results.

'strings' Package in Go: Your Go-To for String Operations'strings' Package in Go: Your Go-To for String OperationsMay 14, 2025 am 12:17 AM

ThestringspackageinGoisessentialforefficientstringmanipulation.1)Itofferssimpleyetpowerfulfunctionsfortaskslikecheckingsubstringsandjoiningstrings.2)IthandlesUnicodewell,withfunctionslikestrings.Fieldsforwhitespace-separatedvalues.3)Forperformance,st

Go bytes package vs strings package: Which should I use?Go bytes package vs strings package: Which should I use?May 14, 2025 am 12:12 AM

WhendecidingbetweenGo'sbytespackageandstringspackage,usebytes.Bufferforbinarydataandstrings.Builderforstringoperations.1)Usebytes.Bufferforworkingwithbyteslices,binarydata,appendingdifferentdatatypes,andwritingtoio.Writer.2)Usestrings.Builderforstrin

How to use the 'strings' package to manipulate strings in Go step by stepHow to use the 'strings' package to manipulate strings in Go step by stepMay 13, 2025 am 12:12 AM

Go's strings package provides a variety of string manipulation functions. 1) Use strings.Contains to check substrings. 2) Use strings.Split to split the string into substring slices. 3) Merge strings through strings.Join. 4) Use strings.TrimSpace or strings.Trim to remove blanks or specified characters at the beginning and end of a string. 5) Replace all specified substrings with strings.ReplaceAll. 6) Use strings.HasPrefix or strings.HasSuffix to check the prefix or suffix of the string.

Go strings package: how to improve my code?Go strings package: how to improve my code?May 13, 2025 am 12:10 AM

Using the Go language strings package can improve code quality. 1) Use strings.Join() to elegantly connect string arrays to avoid performance overhead. 2) Combine strings.Split() and strings.Contains() to process text and pay attention to case sensitivity issues. 3) Avoid abuse of strings.Replace() and consider using regular expressions for a large number of substitutions. 4) Use strings.Builder to improve the performance of frequently splicing strings.

What are the most useful functions in the GO bytes package?What are the most useful functions in the GO bytes package?May 13, 2025 am 12:09 AM

Go's bytes package provides a variety of practical functions to handle byte slicing. 1.bytes.Contains is used to check whether the byte slice contains a specific sequence. 2.bytes.Split is used to split byte slices into smallerpieces. 3.bytes.Join is used to concatenate multiple byte slices into one. 4.bytes.TrimSpace is used to remove the front and back blanks of byte slices. 5.bytes.Equal is used to compare whether two byte slices are equal. 6.bytes.Index is used to find the starting index of sub-slices in largerslices.

Mastering Binary Data Handling with Go's 'encoding/binary' Package: A Comprehensive GuideMastering Binary Data Handling with Go's 'encoding/binary' Package: A Comprehensive GuideMay 13, 2025 am 12:07 AM

Theencoding/binarypackageinGoisessentialbecauseitprovidesastandardizedwaytoreadandwritebinarydata,ensuringcross-platformcompatibilityandhandlingdifferentendianness.ItoffersfunctionslikeRead,Write,ReadUvarint,andWriteUvarintforprecisecontroloverbinary

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools