#Help with go slices.

9 messages · Page 1 of 1 (latest)

plucky topaz
#

This is the code im working with:

#
package main

import (
    "encoding/json"
    "fmt"
    "log"
    "math/rand"
    "os"
    "time"
)

type Config struct {
    Length  string `json:"Length"`
    Digits  bool   `json:"Digits"`
    Letters bool   `json:"Letters"`
    Symbols bool   `json:"Symbols"`
}

type Config_Symbol struct {
    Letters string `json:"Letters"`
    Digits  string `json:"Numbers"`
    Symbols string `json:"Symbols"`
}

var config Config
var config_Symbolen Config_Symbol

func main() {
    var err error

    err = Load_Config("config.json")
    if err != nil {

        log.Fatal("Cannot open the given filename", err)
    }

    err = Load_ConfigSymbols("Config_Symbolen.json")
    if err != nil {
        log.Fatal("Cannot open the given filename for Symbol file", err)
    }

    test := []string(config_Symbolen.Digits, config_Symbolen.Symbols) xxxxxxxxxxxxxxxxxxxx

    rand.Seed(time.Now().UnixNano())
    Generator(test, test2)

}

func Load_Config(filename string) error {

    configFile, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer configFile.Close()
    jsonParser := json.NewDecoder(configFile)
    err = jsonParser.Decode(&config)
    return err
}

func Load_ConfigSymbols(filename string) error {

    configFile, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer configFile.Close()
    jsonParser := json.NewDecoder(configFile)
    err = jsonParser.Decode(&config_Symbolen)
    return err
}

func Generator(flags []string, testing []bool) {
    var builder []string

    fmt.Println(flags)
    for i, flag := range testing {
        if flag {
            switch i {
            case 0:
                builder = append(builder, config_Symbolen.Digits)
            case 1:
                builder = append(builder, config_Symbolen.Letters)
            case 2:
                builder = append(builder, config_Symbolen.Symbols)
            default:

            }

        }

    }

    fmt.Println(builder)

}
#

The problem is at the multiple x's, im making a string slice with in it two strings.
but i get:

too many arguments in conversion
how can i fix this and is my code efficient, im making a password generator.

pliant silo
#

[]string{ … }, not []string( … )

#

the latter is a type conversion, the former is a slice literal

plucky topaz
#

oooh i totally didnt see that

#

thanks man!

#

does my code look okay or?

#

is there any tip u could give me