Hi, I'm Ataias Reis

Programmer, made in Brazil, Alma mater University of Brasilia

30 days of code in Go: Day 1 - summing numbers and concatenating strings

The goal of this second day is the following:

  1. Declare 3 variables: an int, a double and a string.
  2. Read 3 lines from stdin and save them in the variables you just defined
  3. Do some operations with the pre-defined $i$, $d$ and $s$ variables
    1. Print the sum of the integer $i$ with your integer variable
    2. Print the sum of the float $d$ with your double variable with one decimal place
    3. Concatenate $s$ with your string and print the result

My solution was the following

package main

import (

func main() {
 var i uint32 = 4
 var d float32 = 4.0
 var s string = "HackerRank "

 scanner := bufio.NewReader(os.Stdin)

 // Declaring my 3 variables
 var my_i uint32
 var my_d float64
 var my_s string

 // notice: I can't use "\n" (double quotes)
 //Pay attention to use ReadString, not Readstring
 my_s, _ = scanner.ReadString('\n')

 fmt.Println(i + my_i)
 // I tried ((float64) d) first (like C), but didn't work
 fmt.Printf("%.1f\n", float64(d)+my_d) //notice printf to use decimal place
 fmt.Println(s + my_s)

Well, this one got me on some points and I wrote about them in the comments. Some of my mistakes from yesterday helped to use write Scanln.

That’s all for today! Thanks for reading!

comments powered by Disqus