Go Basics: Value Types Exploration

GolangGolangBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab aims to test your understanding of Golang's basic value types, including strings, integers, floats, and booleans.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/BasicsGroup(["`Basics`"]) go/BasicsGroup -.-> go/values("`Values`") subgraph Lab Skills go/values -.-> lab-15526{{"`Go Basics: Value Types Exploration`"}} end

Value Types

Your task is to complete the calculate function that takes in two integers and returns their sum and product.

  • The calculate function should take in two integers as parameters.
  • The calculate function should return two integers, the sum and product of the input parameters.
$ go run values.go
golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

There is the full code below:

// Go has various value types including strings,
// integers, floats, booleans, etc. Here are a few
// basic examples.

package main

import "fmt"

func main() {

	// Strings, which can be added together with `+`.
	fmt.Println("go" + "lang")

	// Integers and floats.
	fmt.Println("1+1 =", 1+1)
	fmt.Println("7.0/3.0 =", 7.0/3.0)

	// Booleans, with boolean operators as you'd expect.
	fmt.Println(true && false)
	fmt.Println(true || false)
	fmt.Println(!true)
}

Summary

In this lab, you were tasked with completing the calculate function to calculate the sum and product of two integers. By doing so, you were able to demonstrate your understanding of Golang's basic value types.

Other Golang Tutorials you may like