FAQ
I have some experience with C and Java and I am trying to learn Go. So I
decided to do a little project envolving a file containing data, a index
hashmap that uses the data name to get the data offset in the file. I've
modelled it in java(it isnt finished).

So, I want to know how to design this problem using golang(I want my code
to be reusable, if I want to use diferent types of data and maybe even
different keys.)

public class FileManager {
     private HashMap<String, Integer> indexMap;
     private ObjectInputStream dataInputStream;
     public FileManager(String dataPath, String indexPath) throws IOException, ClassNotFoundException {
         ObjectInputStream indexInputStream = new ObjectInputStream(new FileInputStream(indexPath));
         indexMap = (HashMap<String, Integer>) indexInputStream.readObject();
         dataInputStream = new ObjectInputStream(new FileInputStream(dataPath));
     }
     public Data getData(Data data){
         int position;
         Data readData;
         position = indexMap.get(data);
         /* Todo: Read object starting at offset position from dataInputStream*/
         return readData;
     }}public abstract class Data {
     String name;
     public String getKey(){
        return name;
     }}public class SampleData extends Data {
     private String name;
     /* Some really big array*/
     private int x;
     private int y;
     private int z;}I have some experience with C and Java and I am trying to learn Go. So I decided to do a little project envolving a file containing data, a index hashmap that uses the data name to get the data offset in the file. I've modelled it in java(it isnt finished).

public class FileManager {
     private HashMap<String, Integer> indexMap;
     private ObjectInputStream dataInputStream;
     public FileManager(String dataPath, String indexPath) throws IOException, ClassNotFoundException {
         ObjectInputStream indexInputStream = new ObjectInputStream(new FileInputStream(indexPath));
         indexMap = (HashMap<String, Integer>) indexInputStream.readObject();
         dataInputStream = new ObjectInputStream(new FileInputStream(dataPath));
     }
     public Data getData(Data data){
         int position;
         Data readData;
         position = indexMap.get(data);
         /* Todo: Read object starting at offset position from dataInputStream*/
         return readData;
     }
}
public abstract class Data {
     String name;
     public String getKey(){
        return name;
     }
}
public class SampleData extends Data {
     private String name;
     /* Some really big array*/
     private int x;
     private int y;
     private int z;
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jesse McNelis at Jun 5, 2013 at 12:31 am

    On Wed, Jun 5, 2013 at 9:46 AM, wrote:

    So, I want to know how to design this problem using golang(I want my code
    to be reusable, if I want to use diferent types of data and maybe even
    different keys.)
    You haven't described the problem. You've only described the solution in
    Java.
    Unless we know the constraints of the problem you're trying to solve it's
    difficult to offer solutions.



    - Jesse
    =====================
    http://jessta.id.au

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Arrow at Jun 5, 2013 at 12:37 am
    I'd suggest writing out the structs and attached methods and worrying about
    the code reuse later. Go makes it easy to reuse code with minimal change as
    this old blog post<http://blog.golang.org/2010/04/json-rpc-tale-of-interfaces.html>illustrates. Use concurrency to separate code in a logical fashion. I would
    likely write a FileManager goroutine that you call with an initialization
    function then call another function to access the data.

    struct FileManager {
         ***Whatever you need to put here
    }

    func Start(filename string) (*FileManager) {
         ***You'd open the file here and do any other initialization you need.
    You can also call an anonymous goroutine if that's what you decide to do.
         go func() { *** goroutine code that waits until you need data then
    fetches it for you}
    }

    func (*FileManager) GetData(name string) (data Data) { *** }

    Getting this running would already point you in the right direction.
    Extending it would just be a matter of placing interfaces at the right
    spot, which you don't even have to rewrite most of your existing code for.
    Let me know if this helps.

    Cheers,

    Arrow

    On Tuesday, June 4, 2013 6:46:43 PM UTC-5, cassiano...@gmail.com wrote:

    I have some experience with C and Java and I am trying to learn Go. So I
    decided to do a little project envolving a file containing data, a index
    hashmap that uses the data name to get the data offset in the file. I've
    modelled it in java(it isnt finished).

    So, I want to know how to design this problem using golang(I want my code
    to be reusable, if I want to use diferent types of data and maybe even
    different keys.)

    public class FileManager {
    private HashMap<String, Integer> indexMap;
    private ObjectInputStream dataInputStream;
    public FileManager(String dataPath, String indexPath) throws IOException, ClassNotFoundException {
    ObjectInputStream indexInputStream = new ObjectInputStream(new FileInputStream(indexPath));
    indexMap = (HashMap<String, Integer>) indexInputStream.readObject();
    dataInputStream = new ObjectInputStream(new FileInputStream(dataPath));
    }
    public Data getData(Data data){
    int position;
    Data readData;
    position = indexMap.get(data);
    /* Todo: Read object starting at offset position from dataInputStream*/
    return readData;
    }}public abstract class Data {
    String name;
    public String getKey(){
    return name;
    }}public class SampleData extends Data {
    private String name;
    /* Some really big array*/
    private int x;
    private int y;
    private int z;}I have some experience with C and Java and I am trying to learn Go. So I decided to do a little project envolving a file containing data, a index hashmap that uses the data name to get the data offset in the file. I've modelled it in java(it isnt finished).

    public class FileManager {
    private HashMap<String, Integer> indexMap;
    private ObjectInputStream dataInputStream;
    public FileManager(String dataPath, String indexPath) throws IOException, ClassNotFoundException {
    ObjectInputStream indexInputStream = new ObjectInputStream(new FileInputStream(indexPath));
    indexMap = (HashMap<String, Integer>) indexInputStream.readObject();
    dataInputStream = new ObjectInputStream(new FileInputStream(dataPath));
    }
    public Data getData(Data data){
    int position;
    Data readData;
    position = indexMap.get(data);
    /* Todo: Read object starting at offset position from dataInputStream*/
    return readData;
    }
    }
    public abstract class Data {
    String name;
    public String getKey(){
    return name;
    }
    }
    public class SampleData extends Data {
    private String name;
    /* Some really big array*/
    private int x;
    private int y;
    private int z;
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 4, '13 at 11:54p
activeJun 5, '13 at 12:37a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase