Last active
July 1, 2021 11:54
-
-
Save IamNator/a58eec84ecbd2371191557596657bb37 to your computer and use it in GitHub Desktop.
solution to interview task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Cement struct { | |
NoOfCement int | |
} | |
func (c *Cement) BuyCement(howmany int) { | |
c.NoOfCement += howmany | |
} | |
func (c *Cement) SellCement(howmany int) { | |
c.NoOfCement -= howmany // o was undefined here. | |
} | |
func (c Cement) String() string { //Cement is now passed by value here | |
return fmt.Sprintf("%v", c.NoOfCement) | |
} | |
func main() { | |
var cement Cement | |
cement.BuyCement(15) | |
cement.SellCement(9) | |
fmt.Println(cement) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Cement struct { | |
NoOfCement int | |
} | |
func (c *Cement) BuyCement(howmany int) { | |
c.NoOfCement += howmany | |
} | |
func (c *Cement) SellCement(howmany int) { | |
c.NoOfCement -= howmany // o is undefined here. | |
} | |
func (c *Cement) String() string { //Cement is now passed by reference here | |
return fmt.Sprintf("%v", c.NoOfCement) | |
} | |
func main() { | |
var cement Cement | |
cement.BuyCement(15) | |
cement.SellCement(9) | |
fmt.Println(&cement) //to call the (* Cement)String here, you have to pass the pointer to Cement | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment