# Calculate the Third Angle of a Triangle in Go

## The challenge

You are given two interior angles (in degrees) of a triangle.

Write a function to return the 3rd.

Note: only positive integers will be tested.

## The solution in Golang

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```package solution

func OtherAngle(a int, b int) int {
return 180-a-b
}
```Code language: Go (go)```

Option 2:

``````package solution

func OtherAngle(a int, b int) int {
if a + b > 180{
return -1
}
return 180 - a - b
}
```Code language: Go (go)```

Option 3:

``````package solution

import "math"

func OtherAngle(a int, b int) int {
const fullNumber = 180
var thirdNumber int = int(float64(fullNumber) - (math.Abs(float64(a)) + math.Abs(float64(b))))
return thirdNumber
}
```Code language: Go (go)```

## Test cases to validate our solution

``````package test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "my/<meta charset="utf-8">solution"
)

var _ = Describe("Basic Tests", func() {
It("OtherAngle(30, 60)", func() { Expect(OtherAngle(30, 60)).To(Equal(90)) })
It("OtherAngle(60, 60)", func() { Expect(OtherAngle(60, 60)).To(Equal(60)) })
It("OtherAngle(43, 78)", func() { Expect(OtherAngle(43, 78)).To(Equal(59)) })
It("OtherAngle(10, 20)", func() { Expect(OtherAngle(10, 20)).To(Equal(150)) })
})
```Code language: Go (go)```
Tags:
Subscribe
Notify of
1 Comment
Inline Feedbacks