Variables, types and constants in Go

Learn Go · Beginner · 10 min · After Install Go and run your first program

How does Go know the type of a variable?

  1. 1Guess

    package main
    
    import "fmt"
    
    func main() {
    	x := 7 / 2
    	y := 7 / 2.0
    	fmt.Println(x, y)
    }
    
    What does this program print?
  2. 2Read

    var and :=

    Declare a variable with var name type, or let Go infer the type: var name = "tea". Inside a function the short form x := 7 does the same and is what most Go code uses. A variable you declare without a value gets its zero value: 0 for numbers, "" for strings, false for booleans.

  3. 3Code

    Types and zero values

    1 / 2
    package main
    
    import "fmt"
    
    func main() {
    	var count int
    	price := 9.5
    	var name = "tea"
    	var ready bool
    	fmt.Printf("%T %v\n", count, count)
    	fmt.Printf("%T %v\n", price, price)
    	fmt.Printf("%T %q\n", name, name)
    	fmt.Printf("%T %v\n", ready, ready)
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Conversions and constants

    Go never converts between number types for you. To mix an int and a float64, convert one: float64(n). Strings and numbers convert with the strconv package: strconv.Itoa turns an int into text and strconv.Atoi parses text into an int and an error. A const declares a value that never changes.

  5. 5Code

    Convert, use a const and parse text

    1 / 2
    package main
    
    import (
    	"fmt"
    	"strconv"
    )
    
    const taxRate = 0.2
    
    func main() {
    	items := 3
    	net := float64(items) * 4.5
    	fmt.Println(net, net*(1+taxRate))
    	label := "Items: " + strconv.Itoa(items)
    	fmt.Println(label)
    	n, err := strconv.Atoi("42")
    	fmt.Println(n+1, err)
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    package main
    
    import "fmt"
    
    func main() {
    	var total int = 10
    	var ratio float64 = 0.5
    	fmt.Println(total * ratio)
    }
    
    The compiler rejects this program. What is the bug?
  7. 7Say it back

  8. 8Mission · 8 min

    Write a program that declares an int, a float64, a string and a bool without values, prints them with %v, then converts the int to float64 and multiplies it by the float.

    go run .

Free AI coding tools to practise with

  1. AgentRouter Free Credits

    Free AI coding credits: $25 per daily check-in and $50 per invite

    No cardVerified

  2. Cursor Free Plan

    Cursor Agent with limited agent and chat requests, $0/month

    No cardVerified

  3. Cline Free Models

    Try select models free in Cline via rotating promotions with a usage quota

    No cardVerified

All free ai coding tools →

Common questions

How does Go know the type of a variable?
When you write var name = "tea" or name := "tea", Go takes the type from the value on the right, here string. This is called type inference. You can also name the type yourself, as in var count int. Either way, the variable keeps that single type for its whole life, and the compiler rejects code that mixes types.
What is the difference between var and := in Go?
Both declare a variable. The var form works anywhere, including at package level, and lets you give a type with or without a value. The short form := needs a value and works only inside functions, which makes it the common choice for local variables. Use var when you want a zero value or an explicit type.
How do I convert an int to a float64 in Go?
Write the type name like a function: float64(n) turns the int n into a float64. Go does not convert numbers for you, so you must do it before mixing an int and a float64 in one expression. To go the other way use int(x), which drops the fraction. To turn a number into text, use the strconv package.

Sources

Measured with go1.27.4 darwin/arm64 on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer