I'm re-learning Go and building a program to create characters for role-playing games. The program uses text files to build lists, and I'd like to let the user change the lists. For example, a modern day Earth game would only have human characters, but a Star Trek (TNG) game might have other species options, and a Star Wars game might have an even different set.
The logic is: 1. If run as provided, the program uses a default directory and default text files. 2. A user can modify one of the default text files to suit their needs. 3. A user can specify a custom directory for text files. 3.1. If a file of the same name as a default file exists in the custom directory, the program will use the custom file. 3.2. If a custom file does not exist, the program will use the default. How would you feel about that, as a user? How can the code and logic be improved? Thanks! Leam ### 1 // Package cli_fallthrough tests setting various vars. 2 3 package main 4 5 import ( 6 "errors" 7 "flag" 8 "fmt" 9 "io/fs" 10 "os" 11 fp "path/filepath" 12 ) 13 14 func exists(filepath string) bool { 15 if _, err := os.Stat(filepath); errors.Is(err, fs.ErrNotExist) { 16 return false 17 } 18 return true 19 } 20 21 func isDir(filepath string) bool { 22 // Need better logic here. What to do if there is an err? 23 dir, _ := os.Stat(filepath) 24 if dir.IsDir() { 25 return true 26 } 27 return false 28 } 29 30 func main() { 31 customData := false 32 datadir := "data" 33 datafiles := map[string]string{ 34 "species": "species.txt", 35 "conflicts": "conflicts.txt", 36 "positives": "positives.txt", 37 } 38 39 custom_datadir := flag.String("custom", "custom", "custom directory for data") 40 flag.Parse() 41 42 if exists(*custom_datadir) { 43 customData = isDir(*custom_datadir) 44 } 45 46 for k, v := range datafiles { 47 datafiles[k] = fp.Join(datadir, v) 48 if customData { 49 filepath := fp.Join(*custom_datadir, v) 50 if exists(filepath) && !isDir(filepath) { 51 datafiles[k] = filepath 52 } 53 } 54 _, err := os.Stat(datafiles[k]) 55 if os.IsNotExist(err) { 56 fmt.Println(err) 57 } 58 fmt.Printf("using %s.\n", datafiles[k]) 59 } 60 } ### -- Systems Programmer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/34b76275-638c-1f80-9e14-81ca6b57c7e0%40gmail.com.