Comments (13)
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.
@halturin Thank you very much for the clarification
from ergo.
@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.
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.
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.
Not sure if I follow the question. The problem is in the way of printing?
from ergo.
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.
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.
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.
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.
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.
Okay, thank you.
from ergo.
wont be fixed.
from ergo.
Related Issues (20)
- GitHub actions fail on all platforms due to integration E2E tests HOT 1
- Unittest `TestDecodeFragment` occasionally fails
- i found rpc:call from erlang to ergo is not working HOT 2
- Ergo receives DIST packet along with the HANDSHAKE final packet. HOT 1
- Second CallRPC method is stuck when it is called against remote erlang node HOT 3
- Does Ergo have a plan to support OTP25/OTP26? HOT 1
- how to use simple_one_for_one under supervisor tree HOT 4
- Panic invalid memory address after reconnect a node HOT 6
- bug for deleteProcess HOT 3
- bug for (*process).UnregisterName HOT 2
- I doubt benchmarks be fair !!! HOT 3
- How to change Logger HOT 2
- gen.TcpHandle Question HOT 10
- When I close it frequently, there will be an unrecoverable panic. HOT 3
- Increase test coverage to 80% HOT 2
- node link problem? How to find HOT 8
- node link problem? HOT 2
- Support for Erlang/OTP 26 HOT 1
- Node.Stop () and node.Wait () will immediately end the process HOT 2
- PoolBehavior does not add restarted process to pool.monitors HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ergo.