GeekTech
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Everything you need to know about Go-Lang - #3 You probably have heard about Google's cool language called Golang. Golang is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is similar to C/C++, but with memory safety, garbage collection, structural typing, and CSP-style concurrency. This is the continuation of [Everything you need to know about Go-Lang - #2](https://geektech.codes/everything-you-need-to-know-about-go-lang-2), so if you have not read that then check that out first. In the last article we learned about **constants**, **operators**, **conditional statements**, **looping statements**, and **structs**. Today we will be going over some conceptual parts of features you already use. ## Data Type You have all heard of **data types**. In simple terms, it is the type of data a variable can store in it. Like `int` only stores values of integer, `float` stores numbers with a decimal, and `string` stores values within quotes. In golang, there are only four basic data types, which are : 1. Integers. 2. Floating Point Numbers. 3. Strings 4. Boolean Values (True/False) ## Integer Numbers without any decimal values are called integers. We can define a variable integer by using the `int` keyword. ```go var a int //declaring an integer type variable ``` Integer has different sub-categories with sizes for numbers of different lengths. Like int32 can store numbers of maximum size 32 bits. Here's the list of all the subcategories of integer and the maximum size of numbers they can store : | Data Types | Size | |:------------- | --------------------- | | `int8`/`byte` | 8-bit signed integer | | `int16` | 16-bit signed integer | | `int32` | 32-bit signed integer | | `int64` | 64-bit signed integer | | Data Types | Size | |:-------------- | ----------------------- | | `uint8`/`byte` | 8-bit unsigned integer | | `uint16` | 16-bit unsigned integer | | `uint32` | 32-bit unsigned integer | | `uint64` | 64-bit unsigned integer | **Note: Signed Integer can hold both negative as well as positive values, but unsigned once can only store positive values**. **Both `int` and `uint` can store values up to 32 bits or 64 bits, depending on if you are using 32 bits or 64 bits operating system respectively. `runes` are a synonym of int32, it also represents Unicode points**. ```go package main import "fmt" func main() { const a int32 = 42424682 const b int16 = 3223 const c byte = 2 fmt.Printf("a: %T, b: %T, c: %T", a, b, c) // %T in printf function returns the type of the variable } ``` Output : ```shell a: int32, b: int16, c: uint8 ``` *Note: You cannot assign a value of int type variable to an int8, int16, or int32 type variable, as Golang doesn't implicitly typecase values of higher data type to that of a lower one.* ## Floating Point Number Floating-point numbers are those numbers with decimals. We can define a floating-point number by using the `float32` or `float64` keyword. Like : ```go var b float64 var c float32 ``` Float has two sub-categories, those are : | Data Type | Size | | --------- | ------------------------------------- | | `float32` | 32-bit IEEE 754 floating-point number | | `float64` | 64-bit IEEE 754 floating-point number | *Note : The **IEEE Standard for Floating-Point Arithmetic** (**IEEE 754**) is a [technical standard](https://en.wikipedia.org/wiki/Technical_standard "Technical standard") for [floating-point arithmetic](https://en.wikipedia.org/wiki/Floating-point_arithmetic "Floating-point arithmetic") established in 1985 by the [Institute of Electrical and Electronics Engineers](https://en.wikipedia.org/wiki/Institute_of_Electrical_and_Electronics_Engineers "Institute of Electrical and Electronics Engineers") (IEEE). The standard [addressed many problems](https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754_design_rationale "Floating-point arithmetic") found in the diverse floating-point implementations that made them difficult to use reliably and [portably](https://en.wikipedia.org/wiki/Software_portability "Software portability"). Many hardware [floating-point units](https://en.wikipedia.org/wiki/Floating-point_unit "Floating-point unit") use the IEEE 754 standard.* ```go package main import "fmt" func main() { var b float64 = 3445.2335 var c float32 = 434.83 fmt.Printf(" b: %T, c: %T\n", b, c) // %T in printf function returns the type of the variable } ``` Output : ```shell b: float64, c: float32 ``` ### Complex 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 parts and the in-built imaginary and real function extract those parts. | Data Type | Description | | ------------ | ----------------------------------------------------- | | `complex64` | Contains `float32` as a real and imaginary component. | | `complex128` | Contain `float64` as a real and imaginary component. | ```go package main import "fmt" func main() { var b complex64 = complex(45, 3) var c complex128 = complex(23, 5) fmt.Printf(" Value of b:%v of %T, Value of c: %v of %T\n", b, b, c, c) } ``` Output : ```shell Value of b:(45+3i) of complex64, Value of c: (23+5i) of complex128 ``` ## Boolean Type Now oftentimes, in programming, all we have to deal with is true or false. Based on different conditions we perform different tasks. We could just use integer values or strings to perform tasks that boolean performs, but using boolean makes it more flexible and sensible. To use a boolean type we use the `bool` keyword. Boolean types only store `true` and `false`. Eg : ```go package main import "fmt" func main() { var isTrue bool = true var isFalse bool = false if isTrue { fmt.Println(isTrue) } else { fmt.Println(isFalse) } } ``` Output : ```shell true ``` ## Strings 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, which 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. Like : ```go package main import "fmt" func main() { var variable string = "GeekTech is" var newVar string = " the best place to learn golang" fmt.Println(variable, newVar) } ``` Output : ```shell GeekTech is the best place to learn golang ``` The `len(string)` function returns the length of a string. Like : ```go package main import "fmt" func main() { newVar := "Helo" fmt.Println(len("Kello")) fmt.Println(len(newVar)) } ``` Output : ```shell 5 4 ``` You can get a single character from a string by using the index numbers. Everything inside a quote is counts as a character be it a letter, space, newline character, number or special character. Index of a string always starts with 0 and ends with number one less than its length. Like a string `hello`, in its 0th index contains the character `h` and the last `o` is contained in the 4th index. Like : ```go package main import "fmt" func main() { newVar := "Helo" fmt.Println(newVar[2]) } ``` Now, what do you think will be the output? `l` ? `e`? Let's see : Output : ```shell 108 ``` Confusing right? Because it returned a random number. Actually, it's not a random number. 108 is the Unicode representation of `l`, or runes in the case of Go. So when we are extracting characters from a string, we are actually extracting the Unicode representation of it. So how do you get back the string representation of it? We just need to typecast it explicitly back to string. ```go package main import ( "fmt" ) func main() { newVar := "Helo" fmt.Println(string(newVar[2])) } ``` Output : ```shell l ``` Now, what if you have numbers in a string and want to convert them back to integer how shall you do it? For that we need, to use `strconv` package. We use `strconv.Atoi(string)`. For example : ```go package main import ( "fmt" "strconv" ) func main() { newVar := "56" variable := "34" fmt.Println(strconv.Atoi(newVar)) fmt.Println(strconv.Atoi(variable)) } ``` Output : ```shell 56 <nil> 34 <nil> ``` Now, what does `<nil>` means over here? `strconv.Atoi()` returns the value in `int` and an error. In this case, there are no errors hence it prints nil. Similarly, if you want to convert integer to string use `strconv.Itoa` function. ```go package main import ( "fmt" "strconv" ) func main() { newVar := 65 variable := 45 fmt.Println(strconv.Itoa(newVar)) fmt.Println(strconv.Itoa(variable)) } ``` Output : ```shell 65 45 ``` **Note: All data types of Golang discussed above are of primitive data types.** ## Your Turn Venture Go Libraries for [Strings](https://pkg.go.dev/strings) , and try out different functions like `len()` or `range()` ## Conclusion This was a short tutorial, however, sometimes it's important to get certain fundamentals right. In this tutorial, we learned about data types. In the next edition, we shall be covering functions, pointers, and user input. ### Thanks For Reading, Follow [us]() for more great articles. Until Next Time, Arindol Sarkar.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully