Converting something to bytes - arrays

I want to convert a variable (I took an int for this example) to a byte using this code that I have found:
func IntToByteArray(num int64) []byte {
size := int(unsafe.Sizeof(num))
arr := make([]byte, size)
for i := 0 ; i < size ; i++ {
byt := *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(&num)) + uintptr(i)))
arr[i] = byte(byt)
}
return arr
}
func main(){
println(IntToByteArray(1456))
}
But the output that it gives me is this one : [8/8]0xc00001a0d0
Can some one explain me why do I have this has a result?
And what is exactly a byte array?

package main
import "fmt"
func IntToByteArray(num int64) []byte {
r := make([]byte, 8)
for i := 0; i < len(r); i++ {
r[i] = byte(num >> (i * 8) & 255)
}
return r
}
func main() {
fmt.Println(IntToByteArray(65280))
}
This assumes little-endianness.
As others have suggested, the included packages are more flexible and tested.

Related

How to sort an array

I have problem for sorting my array in Go,
Here is my code :
func main() {
fmt.Print("Masukkan Jumlah Data yang akan dimasukkan: ")
var jumlahdata int
fmt.Scanln(&jumlahdata)
var DataDiagram = make([]int, jumlahdata)
fmt.Print("Masukkan data secara berurutan dengan spasi sebagai pemisah antar angka: ")
for i := 0; i < jumlahdata; i++ {
fmt.Scanf("%d", &DataDiagram[i])
}
fmt.Print("\n")
var max int = DataDiagram[0]
for _, value := range DataDiagram { // Menemukan nilai maximum
if value > max {
max = value
}
}
var mem int
Sorting(DataDiagram, jumlahdata, mem, max)
}
func Grafik(jumlahdata int, max int, DataDiagram []int) {
for i := max; i >= 1; i-- { // membuat Data Diagram
for j := 0; j < jumlahdata; j++ {
if DataDiagram[j] >= i {
fmt.Print(" | ")
} else {
fmt.Print(" ")
}
}
fmt.Print("\n")
}
for i := 0; i < jumlahdata; i++ {
fmt.Print("---")
}
fmt.Print("\n")
fmt.Print(" ")
for i := 0; i < jumlahdata; i++ {
fmt.Print(DataDiagram[i], " ")
}
}
func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {
for langkah := 0; langkah < (jumlahdata-1) ; langkah++ {
Grafik(jumlahdata, max, DataDiagram)
for i := 0; i < jumlahdata - (langkah-1); i++ {
if DataDiagram[i] > DataDiagram[i + 1] {
mem := DataDiagram[i];
DataDiagram[i] = DataDiagram[i + 1]
DataDiagram[i + 1] = mem;
}
}
}
}
What i expect is look like this:
What I Expect
But the output said otherwise, it give me error : It give me error
Can someone give some guide how to fix this :) i just learn Go yesterday, it similiar to C, but keep giving me index out of range error
I understand your task is to sort an int "array" (slice, in go-speak), showing each step of your work as a graph. Because you must show your work, you can't use go's built-in sorting, e.g. sort.Ints(DataDiagram).
Your problems are with the Sorting function.
Step 1 Your immediate crash-causing problem is that i eventually iterates to a number larger than upper index of DataDiagram. That we fix in the commented line below.
// Step 1: fix the iterator
func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {
for langkah := 0; langkah < (jumlahdata-1) ; langkah++ {
Grafik(jumlahdata, max, DataDiagram)
for i := 0; i < jumlahdata - 1; i++ { // Was: for i := 0; i < jumlahdata - (langkah-1); i++ {
if DataDiagram[i] > DataDiagram[i + 1] {
mem := DataDiagram[i];
DataDiagram[i] = DataDiagram[i + 1]
DataDiagram[i + 1] = mem;
}
}
}
}
Step 2 The code no longer crashes, but is not guaranteed to sort, because it makes only one pass through the inputs. We need to continue looping until there's no more swapping taking place. That problem is fixed below. The code now produces the expected output on the playground.
// Step 2: loop until sorted
func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {
swapped := true
for swapped {
Grafik(jumlahdata, max, DataDiagram)
swapped = false
for i := 0; i < jumlahdata - 1; i++ {
if DataDiagram[i] > DataDiagram[i + 1] {
mem := DataDiagram[i];
DataDiagram[i] = DataDiagram[i + 1]
DataDiagram[i + 1] = mem;
swapped = true
}
}
}
}
Step 3 The above code works fine, but perhaps can use some tidying. The end result is unchanged on the playground.
// Step 3: make it prettier
func Sorting(data []int) {
max := data[0]
for _, value := range data { // Menemukan nilai maximum
if value > max {
max = value
}
}
swapped := true
for swapped {
Grafik(len(data), max, data)
swapped = false
for i := 0; i < len(data)-1; i++ {
if data[i] > data[i+1] {
data[i], data[i+1] = data[i+1], data[i]
swapped = true
}
}
}
}
It's much simpler if you would just use
sort.Ints(ints), which you can see here:
https://goplay.space/#i9VIrDG-vL-

How to derive an list of structs from any struct type - getting from interface{} to a variable length slice []interface{}

I try to implement a function taking (any) structure, returning an array of those structures. ReturnArrayOfStory show the idea with a fixed type struct type.
trying to do the same for any type with function ReturnArrayOfX and reflection fails at compile time.
package main
import (
"fmt"
"reflect"
)
type story_t struct {
LANGUAGE string
SPECIES string
}
func ReturnArrayOfStory(x story_t) []story_t {
x1 := x
var a1 []story_t
a1 = append(a1, x1)
a1 = append(a1, x1)
a1 = append(a1, x1)
return a1
}
func ReturnArrayOfX(x interface{}) []interface{} {
x1 := x
v1 := reflect.ValueOf(&x1).Elem()
a1 := []reflect.TypeOf(&x1)
// var a1 []x
a1 = append(a1, x1)
a1 = append(a1, x1)
a1 = append(a1, x1)
//return a1
return a1
}
func main() {
var as1 []story_t
s1 := story_t{"EN", "Prince of Persia"}
as1 = ReturnArrayOfStory(s1)
//as1 := ReturnArrayOfX(s1)
for i := 0; i < len(as1); i++ {
fmt.Printf("%02d %+v\n", i, as1[i])
}
as2 := ReturnArrayOfX(s1)
//as1 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
fmt.Printf("%02d %+v\n", i, as2[i])
}
}
a1 := []reflect.TypeOf(&x1)
main.go:25:8: reflect.TypeOf is not a type
This is a simplified scenario. In reality, I like to read a multitude of struct types from an external data source like a database.
How can I came to my goal with ReturnArrayOfX?
List item Is this possible? If not,why?
There are two solutions to your problem:
First: if you want to return a slice of a type using reflection:
// You cannot return []interface{}, because this function will return [](type of x), and that is not []interface{}
func ReturnArrayOfX(x interface{}) interface{} {
x1 := x
a1 :=
// this creates *[](typeOf x)
reflect.New(reflect.SliceOf(reflect.TypeOf(x)))
// Append the first element to *[](typeof x)
// after this, a1 now points to a slice, not to a slice *
a1 = reflect.Append(a1.Elem(), reflect.ValueOf(x1))
a1 = reflect.Append(a1, reflect.ValueOf(x1))
a1 = reflect.Append(a1, reflect.ValueOf(x1))
//return [](typeof x)
return a1.Interface()
}
You can use this as:
as2 := ReturnArrayOfX(s1)
arr:=as2.([]story_t)
for i := 0; i < len(arr); i++ {
fmt.Printf("%02d %+v\n", i, arr[i])
}
Second: you can return []interface{} without reflection:
func ReturnArrayOfX(x interface{}) []interface{} {
ret:=make([]interface{},0)
ret=append(ret,x)
ret=append(ret,x)
ret=append(ret,x)
}
Then you need to deal with each element of the array:
as2 := ReturnArrayOfX(s1)
for i := 0; i < len(as2); i++ {
fmt.Printf("%02d %+v\n", i, as2[i])
data:=as2[i].(story_t)
}
Here's a generic slice conversion function:
// convertSlice copies the slice in src to the slice pointed to by pdst.
// The concrete values in src must be assignable to the dst elements.
func convertSlice(pdst interface{}, src interface{}) {
dstv := reflect.ValueOf(pdst).Elem()
srcv := reflect.ValueOf(src)
dstv.Set(reflect.MakeSlice(dstv.Type(), srcv.Len(), srcv.Len()))
for i := 0; i < srcv.Len(); i++ {
dstv.Index(i).Set(reflect.ValueOf(srcv.Index(i).Interface()))
}
}
Use it like this:
// Convert []story_t to []interface{}
s0 := []story_t{{"EN", "Prince of Persia"}, {"EN", "Karateka"}}
var s1 []interface{}
convertSlice(&s1, s0)
// Convert []interface{} containing story_t to []story_t
var s2 []story_t
convertSlice(&s2, s1)
Run it on the playground.

Sum of squares in an array using recursion in golang

So my friend gave me this task where the sum of squares of positive numbers must be calculated using recursion.
Conditions - The input will be a string with space separated numbers
This is what I've come so far but this shows a runtime error.
Here is the full error https://ideone.com/53oOjN
package main
import(
'fmt',
'strings',
'strconv'
)
var n int = 4
var sum_of_squares int = 0
func sumOfSquares(strArray []string, iterate int) int{
number, _ := strconv.Atoi(strArray[iterate])
if number > 0 {
sum_of_squares += number*number
}
if iterate == n {
return 0 // just to end the recursion
}
return sumOfSquares(strArray, iterate+1)
}
func main() {
str := "1 2 3 4"
strArray := strings.Fields(str)
result := sumOfSquares(strArray, 0)
fmt.Println(sum_of_squares, result)
}
The rule of thumb in recursion is termination condition. It should exist and it should exist in the right place.
func sumOfSquares(strArray []string, iterate int) int{
if iterate >= len(strArray) {
return sum_of_squares
}
number, _ := strconv.Atoi(strArray[iterate]) //TODO: handle err here
sum_of_squares += number*number
return sumOfSquares(strArray, iterate+1)
}
Just for you information: canonical recursion should not save it's state into global fields. I would suggest using following function signature.
func sumOfSquares(strArray []string, iterate, currentSum int) int{
//...
return sumOfSquares(strArray, iterate+1, sum_of_squares)
}
So that you don't need to store sum_of_squares somewhere. You will just pass it to next function invocation.
package main
import (
"fmt"
"strconv"
"strings"
)
var n int
func sumOfSquares(strArray []string, iterate int) int {
number, _ := strconv.Atoi(strArray[iterate])
if iterate == n {
return number * number
}
return ((number * number) + sumOfSquares(strArray, iterate+1))
}
func main() {
str := "1 2 3 4"
strArray := strings.Fields(str)
n = len(strArray) - 1
result := sumOfSquares(strArray, 0)
fmt.Println(result)
}
Indexing starts from 0, so decrease the length by one.
As #peterSO have pointed out, if strings contain unusual characters, it doesn't work, I didn't post the right answer for getting input because you seem to be beginner, but you can read the input, like this instead.
var inp []byte
var loc int
inp, _ = ioutil.ReadFile(fileName)
//add \n so that we don't end up running out of bounds,
//if last byte is integer.
inp = append(inp, '\n')
func scanInt() (res int) {
if loc < len(inp) {
for ; inp[loc] < 48 || inp[loc] > 57; loc++ {
}
for ; inp[loc] > 47 && inp[loc] < 58; loc++ {
res = res<<3 + res<<1 + (int(inp[loc]) - 48)
}
}
return
}
This is faster and scans integers only, and skips all other unusual characters.
I like to keep it simple. I have some few if conditions as well, but hope you like it.
func sumOfSquares(numArr []string) int {
i, err := strconv.Atoi(numArr[0])
rest := numArr[1:]
//Error checking
if err != nil {
fmt.Println(err)
os.Exit(1)
return 0
}
square := i * i
// negative & last number
if i < 0 && len(rest) == 0 {
return square
}
// negative & not last number
if i < 0 && len(rest) > 0 {
return sumOfSquares(rest)
}
// last man standing
if i >= 0 && len(rest) == 0 {
return square
}
return square + sumOfSquares(rest)
}
DEMO : https://play.golang.org/p/WWYxKbvzanJ

How to print a byte array as binary in golang?

How to print a byte array []byte{255, 253} as binary in Golang?
I.e.
[]byte{255, 253} --> 1111111111111101
Simplest way I have found:
package main
import "fmt"
func main() {
bs := []byte{0x00, 0xfd}
for _, n := range(bs) {
fmt.Printf("% 08b", n) // prints 00000000 11111101
}
}
Playground with this code: https://play.golang.org/p/eVez0vD4pJk
Or use this simple version
func printAsBinary(bytes []byte) {
for i := 0; i < len(bytes); i++ {
for j := 0; j < 8; j++ {
zeroOrOne := bytes[i] >> (7 - j) & 1
fmt.Printf("%c", '0'+zeroOrOne)
}
fmt.Print(" ")
}
fmt.Println()
}
[]byte{0, 1, 127, 255} --> 00000000 00000001 01111111 11111111

In Golang, why such Type Conversion causes Runtime Error: index out of range?

I was doing the exercise of "A Tour of Go", the page I was on is https://tour.golang.org/moretypes/15
And following is my code:
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var ret [][]uint8;
var row []uint8;
for i:=uint8(0);i<uint8(dy);i++ {
row = []uint8 {}
for j:=uint8(0);j<uint8(dx);j++ {
row = append(row, i+j)
}
ret = append(ret, row)
}
return ret
}
func main() {
pic.Show(Pic)
}
When I run these codes, the console throws an error:
panic: runtime error: index out of range
goroutine 1 [running]:
panic(0x18b820, 0x1040a010)
/usr/local/go/src/runtime/panic.go:464 +0x700
golang.org/x/tour/pic.Show(0x1d7948, 0x104000e0)
/go/src/golang.org/x/tour/pic/pic.go:24 +0x540
main.main()
/tmp/sandbox969725880/main.go:19 +0x20
Does anyone have any ideas why does the error occur for this int->uint8 type conversion?Thanks!
uint8 has a max value of 255 (only 8 bits, max 2^8) but dx, dy passed into Pic can have values greater than that (since they're int's, likely 64 bits). Values greater than 255 might get cast to 0 during conversion to uint8. If dy is 256 and it's cast to 0 as i, the outer for loop doesn't execute at all and no items get pushed into the array. Subsequently, when whatever mechanism in "golang.org/x/tour/pic" tries to access the values in the matrix after it's returned, it looks like it's generating an 'index out of range' error because there are literally no indexes in the matrix to access.
Working code:
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var ret [][]uint8
for i := 0; i < dy; i++ {
row := []uint8{}
for j := 0; j < dx; j++ {
row = append(row, uint8(i+j))
}
ret = append(ret, row)
}
return ret
}
func main() {
pic.Show(Pic)
}

Resources