1

I'm trying to use some autocomplete in my code. The idea is that i use Go generate to create the file below (without the main function) so the maps and the structs. The one that uses that generated file could then create a new order using the structs and the provided options

I run into a couple of issues:

  1. When i create a new instance of a class in node i have intelligence of the variables that the class needs. In Go when i do: order := Order{ there is nothing showing up to help the coder select fields.

  2. As you can see i'm trying to create some options that will "translate" Car to 1 because that is needed in the JSON. But i want more readable code and provide all the options. In Node i can just do: TransportTypeOptions.car on a object and when im at the dot i have a dropdown with the 3 options to choose from. So i would like to see the options when i'm at point X: int64(TransportTypeOptions[X])

I hope i'm asking my question clear enough. And using VScode.

The code:

package main

import (
    "encoding/json"
    "fmt"
)

var TransportTypeOptions = map[string]int{
    "car":   1,
    "plain": 2,
    "boot":  3,
}

var RelationTypeOptions = map[string]int{
    "organisation": 1,
    "private":      2,
}

type Order struct {
    OrderNumber   int64   `json:"OrNu"`
    RelationType  int64   `json:"OrPe"`
    TransportType int64   `json:"VaTr"`
    Addres        *Addres `json:"address"`
}

type Addres struct {
    Street      string `json:"street"`
    Housenumber int64  `json:"housenumber"`
}

func main() {
    order := Order{
        OrderNumber:   776655,
        TransportType: int64(TransportTypeOptions["car"]),
        RelationType:  int64(RelationTypeOptions["organisation"]),
        Addres: &Addres{
            Street:      "Somestreet",
            Housenumber: 4453,
        }}
    json, _ := json.MarshalIndent(order, "", "  ")
    fmt.Println(string(json))
}

EDIT: After @Brits comment I changed the maps to:

const (
    Sales_Order_Transport_Car = iota + 1
    Sales_Order_Transport_Plan
    Sales_Order_Transport_Boot
)

const (
    Sales_Order_Relationtype_Organisation = iota + 1
    Sales_Order_Relationtype_Private
)
RemcoE33
  • 1,551
  • 1
  • 4
  • 11
  • Have you installed the Go VSCode extension & gopls and turned on suggestions? – Adrian Jul 21 '22 at 18:51
  • @Adrian yes both. In my post point 1: When is start typing Order....number then it will show up. But then i already know what i'm about to type. All i want is the [dropdown](https://ibb.co/pZh17yy) but then in Go – RemcoE33 Jul 21 '22 at 19:05
  • You appear to asking VSCode to provide a list of possible `string` map keys. I don't believe that this is currently supported (it does try but the list is long!). Implementing such a list would be difficult because keys could be added anywhere in your code (e.g. `TransportTypeOptions[stringVar]=89`). Using a type (e.g. `type TransportTypeOptions int`) with some defined `const` values is probably closer to your Javascript code and VSCode will provide a list if you use that approach (see [the end of [this answer](https://stackoverflow.com/a/14426447/11810946)) – Brits Jul 21 '22 at 20:23
  • did you try to use ctrl + space https://stackoverflow.com/questions/55564130/in-vs-code-how-do-you-make-intellisense-show-all-object-keys-without-typing-a-c – Isaac Weingarten Jul 21 '22 at 22:28
  • use `json, _ := json.MarshalIndent(order, "\t", " ")` for formated json – Isaac Weingarten Jul 21 '22 at 22:29
  • @Brits thank you, i updated my post on how i use it right now – RemcoE33 Jul 22 '22 at 09:32

0 Answers0