2016. szeptember 12., hétfő 7:28:26 UTC+2 időpontban davidmi...@gmail.com a 
következőt írta:
>
> *I have two Golang questions that I hope someone could help me out with.*
>
>
> *1) Am I misunderstanding Golang maps?*
>
>
> *I'm trying to translate a project from PHP to Golang for the websockets 
> and the concurrency. *
>
>
> *However, I can't do this.....*
>
>
> *          var MyArray [string]string*
>
>
> *Instead, I have to do this.....*
>
>
> *          var MyMap map[string]string*
>
>
> *However, since the same array (map) would be used at least once for each 
> call to the Golang code from the Front End, which occurs any time a user 
> sends data to the Back End, I have been led to believe that I would have to 
> lock and unlock the map for each use. Wouldn't that essentially break 
> concurrency (or at least eliminate its benefits??? No other user's function 
> could use this map until the current user's function is done with it. I'd 
> be back to "single threaded" again (concurrently speaking). This would 
> defeat much of the point in my switching the project from PHP to Golang. To 
> maintain concurrency without blocking other users, one would have to avoid 
> using maps. Arrays (maps) are central to programming. How does anyone get 
> by in Golang without using Maps?????*
>
>
> *For example, if I do the following.....*
>
>
> *func MyFunc (whatever) *map[string]string *{*
>
>
>      var MyMap map[string]string
>
>
> *     // Do something with the map*
>
>
> *     return MyMap*
>
>
> *}*
>
>
> *func CallingFunction (whatever) {*
>
>
> *     MyReturnedMap := go  MyFunc(whatever)*
>
>
> *}*
>
>
> *.....would I have to lock and unlock the map or are two separate 
> instances of the map declared?*
>
>
> *Again, am** I misunderstanding Golang maps?*
>
>
>
Use the lock for as short time as possible: only around get and set access! 
Maybe even:
  mMu.RLock()
  v, ok := m[k]
  mMu.RUnlock()

  if ok {...
  if v ...

  mMu.Lock()
  m[k] = v
  mMu.Unlock()

By the way your example does not use the map concurrently, as *MyFunc* will 
return a new map.
And your code won't even compile! (no "v := go f()" syntax in go).



 

> *2) Also, how would one build a multidimensional array with a string index 
> (or even a map of maps) for something like a large file directory where the 
> number of files, folders and levels are unknown at the outset (without 
> repeatedly using the append function that apparently uses memory on an 
> exponential scale of 2^x)? I've figured out how to do 
> map[string]map[string]map[string]string, but one would have to know how 
> many levels there would be, in advance, to be able to use something like 
> that. Also, when I've tried that, I can't put a string on any level except 
> for the last one, since the only data type that any level other than the 
> last one will accept is another map! What if the map turns out to be only 
> three levels deep for one path and five levels deep for another path?** I've 
> tried the following, which I found online, but I can't dynamically add any 
> new folders to the directory with any kind of ease. It seems that 
> initializing the struct is the only way to get data into the struct without 
> declaring a new struct for every manipulation!*
>
>
> *(Below code from 
> **http://stackoverflow.com/questions/19080116/golang-tree-like-solution-for-filesystem
>  
> <http://stackoverflow.com/questions/19080116/golang-tree-like-solution-for-filesystem>)*
>
>
> package Websocket
>
> import "fmt"
>
> type File struct {
>        Name string
> }
>
> type Folder struct {
>        Name    string
>        Files   []File
>        Folders []Folder
> }
>
> func main() {
>        root := Folder{
>               Name: "Root",
>               Files: []File{
>                      {"One"},
>                      {"Two"},
>               },
>               Folders: []Folder{
>                      {
>                             Name: "Empty",
>                      },
>               },
>        }
>        fmt.Printf("Root %#v\n", root)
> }
>
>
> *Given the above, how would i now add another folder to the directory tree. 
> Among many other variations, I've tried the following.....*
>
>
> *root.Folders[0].Name = "MyFirstFolder"*
>
>
> *.....but nothing I have tried has worked.*
>
>
> *What I've been trying to do is set up an array that looks like the 
> following.....*
>
>
> *MyArray["Number of Sub Items"] = 6*
>
> *MyArray["Type of Sub Items"] = "Folders"*
>
> *MyArray[0] = "Folder 1"*
>
> *MyArray[1]** = "Folder 2"*
>
> *MyArray[2]** = "Folder 3"*
>
> *MyArray[3]** = "Folder 4"*
>
> *MyArray[4]** = "Folder 5"*
>
> *MyArray[6]** = "Folder 6"*
>
>
> *MyArray[3]**["Number of Sub Items"]** = 3*
>
> *MyArray["Type of Sub Items"] = "Folders"*
>
> *MyArray[3][0] = "Folder 1"*
>
> *MyArray[3][1]** = "Folder 2"*
>
> *MyArray[3][2]** = "Folder 3"*
>
>
> *Note that this problem is not exclusive to directory structure. Using 
> directory structure is just a simple way to describe the problem I'm having.*
>
>
> *Alternatively**, are there any libraries that can do the above so that I 
> don't have to "reinvent the wheel"?*
>
>
>
For not reinventing the wheel, take a look at http.File, FileSystem,  and 
Dir.

For using a map: why not use the path (with / as separator) as the key?
Or implement a tree with pointers, if you really need it.

What are you wishing to accompish?



-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to