# Conversion from binary to octal in golang

Go program for Conversion from binary to octal. Here mentioned other language solution.

``````package main
import "strconv"
import "fmt"

//  go lang program for
//  Convert Binary number into Octal number

// Convert decimal number into octal
func octal( number  int)int {
var  result  int = 0;
var  multiplier  int = 1;
var  remainder  int = 0;
for(number != 0) {
remainder = number % 8;
result = (remainder * multiplier) + result;
multiplier *= 10;
number = number / 8;
}
return result;
}
func binaryToOctal( num  string) {
if (len(num) == 0) {
// When empty binary number
return;
}
// Some useful variable
var  flag  bool = false;
var  decimalNo  int = 0;
var  counter  int = 0;
var  index  int = len(num) - 1;
// We Assume that given binary number is valid
// Here - indicates negative binary number
// First convert binary to decimal
// Example = 10111 => 23
for(index >= 0) {
if (num[index] == '1') {
decimalNo += (1 << counter);
} else
if (num[index] != '0') {
if (index == 0 && num[index] == '-') {
// When get negative number
flag = true;
} else {
// Not a valid binary number
return;
}
}
counter += 1;
index -= 1;
}
// When given number is
var  output  int = octal(decimalNo);
if (flag == true) {
output = -output;
}
// Display given number
fmt.Print("Binary : " + num);
// Display result
fmt.Println(" Octal : " + strconv.Itoa(output));
}

func main() {

// Test Case
binaryToOctal("1111");
binaryToOctal("10111");
binaryToOctal("101011");
binaryToOctal("11011");
binaryToOctal("-1000110");

}``````

Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.