Advertisement
cwchen

[Go] C-style objects in Go

Sep 30th, 2017
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.75 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. func Point_new(x float64, y float64) *Point {
  13.     p := new(Point)
  14.  
  15.     p.x = x
  16.     p.y = y
  17.  
  18.     return p
  19. }
  20.  
  21. func Point_get_x(p *Point) float64 {
  22.     return p.x
  23. }
  24.  
  25. func Point_get_y(p *Point) float64 {
  26.     return p.y
  27. }
  28.  
  29. func Point_set_x(p *Point, x float64) {
  30.     p.x = x
  31. }
  32.  
  33. func Point_set_y(p *Point, y float64) {
  34.     p.y = y
  35. }
  36.  
  37. func main() {
  38.     p := Point_new(0, 0)
  39.  
  40.     if !(Point_get_x(p) == 0) {
  41.         log.Fatal("Wrong value")
  42.     }
  43.  
  44.     if !(Point_get_y(p) == 0) {
  45.         log.Fatal("Wrong value")
  46.     }
  47.  
  48.     Point_set_x(p, 3.0)
  49.     Point_set_y(p, 4.0)
  50.  
  51.     if !(Point_get_x(p) == 3.0) {
  52.         log.Fatal("Wrong value")
  53.     }
  54.  
  55.     if !(Point_get_y(p) == 4.0) {
  56.         log.Fatal("Wrong value")
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement