Coder Social home page Coder Social logo

Comments (13)

halturin avatar halturin commented on May 15, 2024

Thanks for the good question. RPC call returns the correct value. String in Erlang is the same as a regular list of numbers with values in range 0..255. It means passing through the encoder Erlang VM treats that value as a string.

let me show you how it looks under the hood. here is your example

([email protected])2> T = erlang:statistics(active_tasks).
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0|...]
([email protected])3> term_to_binary(T).
<<131,107,0,65,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,...>>
([email protected])4>

As you may notice, the binary value has the second byte = 107 - this is a string (https://erlang.org/doc/apps/erts/erl_ext_dist.html#string_ext). That's why you see 'string' value on your side (etf.Term, to be specific, which is interface{} actually).

How to work with this kind of data. You just need to make a real string value from etf.Term and cast it to the []byte after all.

// we should make sure that activeTask is a string 
if activeTaskStr, isString := activeTask.(string); isString {
    // and here just treat this value as a []byte
   fmt.Printf("active task: %#v\n", []byte(activeTaskStr))
}

from ergo.

Noksa avatar Noksa commented on May 15, 2024

@halturin Thank you very much for the clarification

from ergo.

Noksa avatar Noksa commented on May 15, 2024

@halturin
I can't figure out how I can receive the result as [0,0,0,1,0,0,0,0,0,0,0,0,0] (for example as string or as int slice).

What do I need to do with []byte?

from ergo.

halturin avatar halturin commented on May 15, 2024
var b []byte
activeTasks, err := process.CallRPCWithTimeout(8, node, "erlang", "statistics", etf.Atom("active_tasks"))
// activeTasks has etf.Term type, which is redefined type of interface{}. 
// You should try to transform it to the 'string' type this way:
if activeTaskStr, isString := activeTasks.(string); isString { // make a string from an interface{} type
    b = []byte(activeTaskStr) // cast given 'string' to the []byte type
    fmt.Printf("active task: %#v\n", b )
}

from ergo.

Noksa avatar Noksa commented on May 15, 2024

Sorry for my stupidity ^^ but I got the following result:

active task: []byte{0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}

So how I can cast this to a slice or just to string as [0,0,0,1,0,0,0,0,0,0,0,0,0]? :)

from ergo.

halturin avatar halturin commented on May 15, 2024

Not sure if I follow the question. The problem is in the way of printing?

from ergo.

Noksa avatar Noksa commented on May 15, 2024

No. The problem is how to use it.

I want to make a slice from this values.
I mean slice of integers 0,0,0,1...

from ergo.

halturin avatar halturin commented on May 15, 2024

why do you think it isn't?
active task: []byte{0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}

this is exactly what are you want. you can access to the random element within this list of bytes like
https://play.golang.org/p/Q8FYd18qY30


import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground")
	active_task:= []byte{0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
	fmt.Printf("active task: %#v\n", active_task)
	b4 := active_task[4]
	fmt.Printf("active task: %#v\n", b4)
	b4_7 := active_task[4:7]
	fmt.Printf("active task: %#v\n", b4_7)
}

output

Hello, playground
active task: []byte{0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
active task: 0x1
active task: []byte{0x1, 0x0, 0x0}

I would recommend you to read a book about this beautiful language. Its super easy to learn, but you definitely should read at least one of them just to get to know basic stuff.

from ergo.

halturin avatar halturin commented on May 15, 2024

Here is useful information about the slices http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html#array_func_args

from ergo.

Noksa avatar Noksa commented on May 15, 2024

Yeah, I understand how I can use it to print bytes... But I want to use them as integers like they look in erlang.

I mean when I run this command in an erlang node I receive the following result:

[0,0,0,1,0,0,0,0,0,0,0,0,0]

It is like a list of integers.

So I want to receive the same result on go. Something like this:

intSlice := make([]int, 0)
...
some code
...
fmt.Printf("%v", intSlice[0])  # == 0
fmt.Printf("%v", intSlice[3])  # == 1

I tried to convert byte to int but it doesn't work as expected :{

from ergo.

halturin avatar halturin commented on May 15, 2024

Golang is a strongly typed language. You can't treat the list of numbers the same way as it can be done in Erlang. In Golang, 'byte' is an alias for the uint8.

I'm sorry, but this is not a good place to learn Golang. I see the gap in terms of understanding the fundamental stuff in Golang.

from ergo.

Noksa avatar Noksa commented on May 15, 2024

Okay, thank you.

from ergo.

halturin avatar halturin commented on May 15, 2024

wont be fixed.

from ergo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.