0

I have a .txt file which contains 3d points of the form (x,y,z). Using go I extract the point co-ordinates into arrays X[], Y[], Z[]. Now I need to pass these arrays to an external javascript (ie a function in js). How do I do this? In general, how do I pass some arguments to any js function in a .html file.

3
  • 4
    What has this got to do with Java? Java and Javascript are completely different languages. Commented Jun 14, 2011 at 7:03
  • Where is the connection between Go and JavaScript here?? Does Go call JavaScript? Does Go provide the JavaScript Source? What does your environment look like?? Commented Jun 15, 2011 at 20:05
  • Actually, I am running a server on go and I have to call a javascript function from this server and also the parameters passed are generated by go. Now the main problem is how do I make these parameters which are generated in go to the js function.... Commented Jun 17, 2011 at 14:21

2 Answers 2

2

I'd say: just pass them (by reference):

function doSomethingWithPpoints(x,y,z){
   //do something with for example x[0], y[1] etc.
}
//later on
doSomethingWithPoints(points1,points2,points3);

[edit] This may be an idea: serialize the array to string an attach that as a querystring to the url:

var url = 'http://somesite.net/somefile.html'+
          '?points1=1,2,3&points2=3,2,1&points35,6,7';

Now in the javascript of somefile.html extract the arrays like this:

var qstr = location.href.split('?')[1],
    points = qstr.split('&')
    pointsObj = {},
    i = 0;
    while ((i = i + 1)<points.length) {
      var point = points[i].split('=');
      pointsObj[point[0]] = point[1].split(',');
    }

this should deliver the Object pointsObj with 3 properties (points1-3) with array values

//pointsObj looks like this
{ points1: [1,2,3],
  points2: [3,2,1],
  points3: [5,6,7] }
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think you got what I am asking, what I am saying is that I have a file "main.go", in which I have written code to extract the arrays x[], y[], z[] from a .txt file. After extracting these arrays from the .txt file I want to pass it to another "index.html" file which contains a js function (say f), which takes as parameters three arrays. Now, how do I pass those extracted arrays x,y,z from main.go to the function f in index.html
0

Assuming that the server you are running is a Go program, you should go the other way around.

The javascript function performs an XHR request to the server, asking for the vector data. The server then optionally reads them from the text file (or already has them in-memory) and sends the data encoded as json back to the client.

index.html:

The 'doXHR' method should do the actual get request to the server. Whatever the implementation of that is, is up to you to figure out. The jquery framework has a $.ajax() method for this very purpose.

 function getData() {
      doXHR({
           uri: "/getvectors", 
           success: function(data) {
                // data now holds the json encoded vector list.
                // Do whatever you want with it here. 
           }
      });
 }

On the Go side:

 func myVectorHandler(w http.ResponseWriter, r *http.Request) {
      var vectors struct {
           X []float32
           Y []float32
           Z []float32
      }

      // Fill the X/Y/Z slices here.

      // Encode it as json
      var data []byte
      var err os.Error
      if data, err = json.Marshal(vectors); err != nil {
           http.Error(w, err.String(), http.StatusInternalServerError)
           return
      }

      // Set the correct content type and send data.
      w.Headers().Set("Content-Type", "application/x-json");
      w.Write(data);
 }

A much simpler solution is to store the vector data in json format in the textfile and serve it to the client as-is. That way your Go server does not have to perform the conversion at runtime.

2 Comments

A much simpler solution is to store the vector data in json format in the textfile and serve it to the client as-is. That way your Go server does not have to perform the conversion at runtime. Could you tell me how could this be done ?
Is there something as a .json file in which i could store the data in json format and then when the user needs, the javascript would be passed the contents of this .json file .... Could you please post some links for reference..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.