u/xfinitystones

▲ 0 r/golang

What is a good way to test list of variables to see if any are equal to a specific value?

I have a function that loops through a CSV file looking for specific field names and captured the index of the field for later data collection.

Unfortunately, the organization (not mine) that creates the csv file is prone to randomly add field or move the data to a different column, but the column names are consistent.

To help detect if they changed a field name or omitted it altogether, what I want to do is set the field index to -1 and then use something like Python's "any()" function to say if any of (list of variables) = -1 after parsing the header row, then log the error and quit :) .

what would be a good way to implement this in Go?

var ( 
     firstIndex int = -1
     secondIndex int = -1
)

for {} // loop checks all of the headers and sets index to the index value.

//psuedo code
check the value of each index variable. if any equal negative one, return error. 

I hope that this makes sense. My thought is that I could put the indexes into a map , slice, struct, etc to make looping easier. but I'm still pretty new to Go. And no, I haven't asked AI yet, but that is a good idea!

Ideas?

reddit.com
u/xfinitystones — 15 hours ago
▲ 13 r/golang

Assign value to struct instead of copy of struct

I'm looping through a slice of "Server" structs , and I want to assign a value to one of their properties. However the assignment goes to a copy and not the actual struct.

the default of none value of the property is false. For some of them, I want to change it to true.

Here is an example of what I want to do, but have the struct and not the copy be changed.

type Server struct {
  Online bool
}

for _, server := range servers {    

  server.Online = true
}

If I pass servers address into to function ( eg. func dosomething(s *Server ) and doSomething(&server) ) and change something , the change persists. Is there a way to do that without using a function?

I tried

&server.Online = true

which didn't work.

Solved

I changed servers into a the slice pointers to server structs. That didn't require much refactoring and got my back on track.

Thank you for the great ideas!

reddit.com
u/xfinitystones — 10 days ago
▲ 10 r/golang

Best way to unmarshalling command line arguments (pflags) into a struct?

I'm using pflags and viper modules for load configuration information from the command line, environment.

I'm loading config from the command line (pflags) , a yaml file (viper), and env variables (viper), and then I'm unmarshalling the configuration into a struct called appConfig.

Where I'm stuck is taking unmarshalling 3 command line arguments into a struct nested inside of the AppConfig struct.

Unfortunately, my code contains employer confidential information, so I can't paste it here. so I wrote up an shorter but equivalent code for my situation.

// global configuration object. Populate configuration from config file, from env vars, and command line parameters.


type DNSConfig struct {
    username string  `mapstructure:"username"`
    password string `mapstructure:"password"`
    ipAddr string  `mapstructure:"ipaddress"`
}


type AppConfig struct {
    dnsConnect           DNSConfig `mapstructure:"what goes here?"`
}


func LoadConfig() (AppConfig, error) {
    var config AppConfig
    v := viper.NewWithOptions(viper.ExperimentalBindStruct())
    pflag.String("username", "", "username")
    pflag.String("password", "", "password")
    pflag.String("ipaddress", "", "ip address")
    pflag.Parse()
    v.BindPFlags(pflag.CommandLine)
    if err := v.Unmarshal(&config); err != nil {
        return config, fmt.Errorf("failed to unmarshal config: %w", err)
    }
}// global configuration object. Populate configuration from config file, from env vars, and command line parameters.


type DNSConfig struct {
    username string  `mapstructure:"username"`
    password string `mapstructure:"password"`
    ipAddr string  `mapstructure:"ipaddress"`
}


type AppConfig struct {
    dnsConnect           DNSConfig `mapstructure:"vinylDNS"`
}


func LoadConfig() (AppConfig, error) {
    var config AppConfig
    v := viper.NewWithOptions(viper.ExperimentalBindStruct())
    pflag.String("username", "", "username")
    pflag.String("password", "", "password")
    pflag.String("ipaddress", "", "ip address")
    pflag.Parse()
    v.BindPFlags(pflag.CommandLine)
    if err := v.Unmarshal(&config); err != nil {
        return config, fmt.Errorf("failed to unmarshal config: %w", err)
    }
}

My work around it create an dnsConfig struct , populate it, and then assign it to the AppConfig dnsConnect property.

That is clunky, in my mind. I'd rather have a more elegant way to do this, such as getting v. unmarshal to do it.

Ideas?

reddit.com
u/xfinitystones — 2 months ago
▲ 5 r/golang

Unmarshalling YAML Config into Struct using Viper - Capitalization?

I'm trying to unmarshal configuration values loaded from a yaml file into a struct . The values are loaded fine as I can see them in v.config in my debugger. But the struct is not populated by by v.unmarshal

The struct fields look like this

clientCertCertFilePath string `json:"actions,omitempty yaml:"clientCertCertFilePath" mapstructure:"clientCertCertFilePath"`    

The data in yaml file looks like this

clientCertCertFilePath: "filename.crt"

and unmarshal command looks like this

v.Unmarshal(&config)v.Unmarshal(&config)

The data looks like this ( redacted for confidential info ) in v.config post-unmarshal

clientcertcertfilepath: "filename.crt"

The AppConfig struct looks like this post-unmarshal

clientcertcertfilepath: ""

You can tell I've tried every tag I can think of to map the values from the file to values in the struct.

I read somewhere I might need to capitalize the struct property names, but I could not find anything in the Viper docs to confirm that.

Any guidance on this?

Update

The issue was the capitalization. being new to Go , I didn't know public / private was controlled by capitalization.

Now I know. But I'm not nearly halfway through the battle. I have to finish the Go programming course and books that I'm reading , and then still read online docs. So much to learn!

reddit.com
u/xfinitystones — 2 months ago
▲ 1 r/Slack

Is there a way to sync Slack Status and Microsoft Teams status?

My employer uses 2 platforms for internal messaging -> Slack and Microsoft Teams. Different organizations use different platforms as their primary messaging platform. Mine uses Slack, but I interact with other organizations that use Teams.

I want my availability in each platform to be the same. So if I switch to Away - In a meeting in Slack, I want my MS Teams status to automatically be the same.

Since these platforms are carrying potentially confidential information, I cannot use public internet services to keep them in sync. Is there something I can do inside the Slack app to sync my availability with MS Teams? or vice versa?

Update

In Slack, I found an Outlook app and connected it to my work outlook account. it has been changing my Slack status when I have a scheduled event, so that part is good. I tested changing my status in Slack to "in a meeting" and to "away" and teams did not change. It appears to only work from Outlook to Slack, or one direction.

I haven't tested if a tentative meeting will appear in Slack or not. I have a few of those tomorrow, so I'll test then.

reddit.com
u/xfinitystones — 3 months ago
▲ 1 r/MacOS

h I ran into this with markdown files ending in .md, since I use several editors with them. Some of them are documentation files in git repositories, and some are notes in my PKM system that I edit with the Obsidian notes application.

Is there a way to Open file from Spotlight Search Results with Specific Application(not the default)? Currently, when I right-click a file in search results , my only option to open the file is "open".

Is there a trick to getting "Open With" to appear in the menu? That way I can use my app of choice.

reddit.com
u/xfinitystones — 3 months ago