Skip to content

Instantly share code, notes, and snippets.

@IamNator
Last active July 1, 2021 11:54
Show Gist options
  • Save IamNator/a58eec84ecbd2371191557596657bb37 to your computer and use it in GitHub Desktop.
Save IamNator/a58eec84ecbd2371191557596657bb37 to your computer and use it in GitHub Desktop.
solution to interview task
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)
}
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