multiple-return-values

how to parse multiple returns in golang

旧巷老猫 提交于 2019-12-07 13:11:39
问题 I have a Go function which returns two integer values. Below is the function func temp() (int, int){ return 1,1 } Is it possible to put temp function directly into a Println and print both the outputs using string formatting as below: fmt.Println("first= %d and second = %d", temp() ) // This doesn't work In Python, I am able to do the following: def func(): return 1,1 print("{0}={1}".format(*func()) >> '1=2' Can I do Something similar in Go too? 回答1: First, for what you attempt to do you

Can constexpr-if-else bodies return different types in constexpr auto function?

ぐ巨炮叔叔 提交于 2019-12-07 09:08:50
问题 I'm trying to write a function that maps an enumeration of values to a set of types based on the runtime value of the enumeration. I realize that you cannot return different types based on the runtime value of an enumeration because the compiler wouldn't know how much stack space to allocate. However I'm trying to write this as a constexpr function, using the new if-constexpr functionality to implement this. I'm getting an error from clang complaining that I'm using an illegally specified

how to parse multiple returns in golang

北战南征 提交于 2019-12-06 02:37:35
I have a Go function which returns two integer values. Below is the function func temp() (int, int){ return 1,1 } Is it possible to put temp function directly into a Println and print both the outputs using string formatting as below: fmt.Println("first= %d and second = %d", temp() ) // This doesn't work In Python, I am able to do the following: def func(): return 1,1 print("{0}={1}".format(*func()) >> '1=2' Can I do Something similar in Go too? First, for what you attempt to do you should use fmt.Printf() instead of fmt.Println() as only the former expects and uses a format string. Going

Can constexpr-if-else bodies return different types in constexpr auto function?

戏子无情 提交于 2019-12-05 15:34:05
I'm trying to write a function that maps an enumeration of values to a set of types based on the runtime value of the enumeration. I realize that you cannot return different types based on the runtime value of an enumeration because the compiler wouldn't know how much stack space to allocate. However I'm trying to write this as a constexpr function, using the new if-constexpr functionality to implement this. I'm getting an error from clang complaining that I'm using an illegally specified template parameter. Does anyone see how to implement this? edit: Here is an easier to grok version

Powershell Join-Path showing 2 dirs in result instead of 1 - accidental script/function output

我们两清 提交于 2019-11-27 07:24:38
问题 I am constructing incremental directory structures, and for some reason Join-Path is showing 2 dirs. When I later join that with a file I'm sending to copy-item, it causes an error, as shown below. I have shown in the comment for the $to_loc_finalDT1 line, where I first see these two dirs: Copy-Item : Cannot find path '\\T2\DisasterBackup\Loc_2019-03-08\Privileges\Privileges_HH_Bak.csv \\T2\DisasterBackup\Loc_2019-03-08\Privileges\Privileges_HH_Bak.csv' because it does not exist So this is

Is it pythonic for a function to return multiple values?

懵懂的女人 提交于 2019-11-26 23:49:40
In python, you can have a function return multiple values. Here's a contrived example: def divide(x, y): quotient = x/y remainder = x % y return quotient, remainder (q, r) = divide(22, 7) This seems very useful, but it looks like it can also be abused ("Well..function X already computes what we need as an intermediate value. Let's have X return that value also"). When should you draw the line and define a different method? jfs Absolutely (for the example you provided). Tuples are first class citizens in Python There is a builtin function divmod() that does exactly that. q, r = divmod(x, y) # (

Return map like 'ok' in Golang on normal functions

≯℡__Kan透↙ 提交于 2019-11-26 11:55:40
In Go, the following works (note one use of the map has one return, the other has two returns) package main import "fmt" var someMap = map[string]string { "some key": "hello" } func main() { if value, ok := someMap["some key"]; ok { fmt.Println(value) } value := someMap["some key"] fmt.Println(value) } However, I have no idea how to do this same thing with my own function. Is it possible to have similar behavior with an optional return like map ? For example: package main import "fmt" func Hello() (string, bool) { return "hello", true } func main() { if value, ok := Hello(); ok { fmt.Println

Is it pythonic for a function to return multiple values?

吃可爱长大的小学妹 提交于 2019-11-26 08:49:34
问题 In python, you can have a function return multiple values. Here\'s a contrived example: def divide(x, y): quotient = x/y remainder = x % y return quotient, remainder (q, r) = divide(22, 7) This seems very useful, but it looks like it can also be abused (\"Well..function X already computes what we need as an intermediate value. Let\'s have X return that value also\"). When should you draw the line and define a different method? 回答1: Absolutely (for the example you provided). Tuples are first

Return map like 'ok' in Golang on normal functions

泪湿孤枕 提交于 2019-11-26 02:38:30
问题 In Go, the following works (note one use of the map has one return, the other has two returns) package main import \"fmt\" var someMap = map[string]string { \"some key\": \"hello\" } func main() { if value, ok := someMap[\"some key\"]; ok { fmt.Println(value) } value := someMap[\"some key\"] fmt.Println(value) } However, I have no idea how to do this same thing with my own function. Is it possible to have similar behavior with an optional return like map ? For example: package main import \

Multiple values in single-value context

不想你离开。 提交于 2019-11-25 23:35:27
问题 Due to error handling in Go, I often end up with multiple values functions. So far, the way I have managed this has been very messy and I am looking for best practices to write cleaner code. Let\'s say I have the following function: type Item struct { Value int Name string } func Get(value int) (Item, error) { // some code return item, nil } How can I assign a new variable to item.Value elegantly. Before introducing the error handling, my function just returned item and I could simply do this