Advertisement
cwchen

[Go] Creating a Point class.

Oct 5th, 2017
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.36 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "log"
  5. )
  6.  
  7. type Point struct {
  8.     X float64
  9.     Y float64
  10. }
  11.  
  12. // function as constructor
  13. func NewPoint(x float64, y float64) *Point {
  14.     p := new(Point)
  15.  
  16.     p.X = x
  17.     p.Y = y
  18.  
  19.     return p
  20. }
  21.  
  22. func main() {
  23.     p := NewPoint(3, 4)
  24.  
  25.     if !(p.X == 3.0) {
  26.         log.Fatal("Wrong value")
  27.     }
  28.  
  29.     if !(p.Y == 4.0) {
  30.         log.Fatal("Wrong value")
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement