CodeBlanks Logo Icon CodeBlanks

Summary - Data types

Combine int, float, string and bool to write a profile card.

Data types summary

Basic types: int (integer), float64 (decimal), bool (true/false), string (UTF-8 text), byte (a byte) and rune (a character). Go doesn't convert types on its own; use explicit conversions like float64(n) and int(x), and strconv for text. With type you can define your own types and add methods to them.

var (
    adet  int     = 3
    fiyat float64 = 12.5
    ad    string  = "Kalem"
    stok  bool    = true
)

Zero values

Every variable declared without a value has a zero value: 0 for numbers, false for bool, and "" for string. That's why Go has no "undefined" variables.

var n int
var s string
var b bool
fmt.Printf("%d %q %t\n", n, s, b)  // 0 "" false

Practice Print the types

Declare adet := 3, fiyat := 12.5, ad := "Kalem", stok := true. Print the types of all four with fmt.Printf("%T %T %T %T\n", ...).

Starter code

main.go
package main

import "fmt"

func main() {
    // buraya yaz
}

Sample solution

main.go
package main

import "fmt"

func main() {
    adet := 3
    fiyat := 12.5
    ad := "Kalem"
    stok := true
    fmt.Printf("%T %T %T %T\n", adet, fiyat, ad, stok)
}

Expected output

int float64 string bool

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which format prints a variable's type?

  • %v
  • %d
  • %s
  • %T Correct answer

Explanation: fmt.Printf("%T", x) prints the variable's Go type (e.g. float64).

2 Print the type

Question
fmt.Printf("____
", 3.5)  // float64
Answer
fmt.Printf("%T
", 3.5)  // float64

Explanation: %T prints the value's type.

3 Type and zero value

Term Meaning
int 0
float64 0 (0.0)
bool false
string ""

All lessons in Beginner