In the Go language,
[]string is a string array
and we also use ...string as a parameter.
What is the difference?
Function definition:
func f(args ...string) {}
Can I call this function like below?
args := []string{"a", "b"}
f(args)
[]string is a string array
Technically it's a slice that references an underlying array
and we also use ...string as a parameter.
What is the difference?
With respect to the structure, nothing really. The data type resulting from both syntax is the same.
The ... parameter syntax makes a variadic parameter. It will accept zero or more string arguments, and reference them as a slice.
With respect to calling f, you can pass a slice of strings into the variadic parameter with the following syntax:
func f(args ...string) {
fmt.Println(len(args))
}
args := []string{"a", "b"}
f(args...)
This syntax is available for either the slice built using the literal syntax, or the slice representing the variadic parameter (since there's really no difference between them).
http://play.golang.org/p/QWmzgIWpF8
Both create an array of strings, but the difference is in how it is called.
func f(args ...string) {
}
// Would be called like this:
f("foo","bar","baz");
This allows you to accept a variable number of arguments (all of the same type)
A great example of this is fmt.Print and friends, which can accept as few or as many arugments as you want.
Here is what you want:
var args []string = []string{"A", "B", "C"}
func Sample(args ...string) {
for _, arg := range args {
fmt.Println(arg)
}
}
func main() {
Sample(args...)
}
Play: http://play.golang.org/p/N1ciDUKfG1
It simplifies your function parameters. Here is an example(https://play.golang.org/p/euMuy6IvaM):
Method SampleEllipsis accepts from zero to many parameters of the same type but in the method SampleArray it is mandatory args to be declared.
package main
import "fmt"
func SampleEllipsis(args ...string) {
fmt.Printf("Sample ellipsis : %+v\n",args)
}
func SampleArray(args []string) {
fmt.Println("Sample array ")
SampleEllipsis(args...)
}
func main() {
// Method one
SampleEllipsis([]string{"A", "B", "C"}...)
// Method two
SampleEllipsis("A", "B", "C")
// Method three
SampleEllipsis()
// Simple array
SampleArray([]string{"A", "B", "C"})
// Simple array
SampleArray([]string{})
}
Returns :
Sample ellipsis : [A B C]
Sample ellipsis : [A B C]
Sample ellipsis : []
Sample array
Sample ellipsis : [A B C]
Sample array
Sample ellipsis : []
Related
In Golang, everything is passed by value. If I pass an array "directly" (as opposed as passing it by pointer), then any modification made in the function will be found outside of it
func f(a []int) {
a[0] = 10
}
func main() {
a := []int{2,3,4}
f(a)
fmt.Println(a)
}
Output: [10 3 4]
This is because, to my understanding, an array constitutes (among other things) of a pointer to the underlying data array.
Unless I am mistaken (see here) strings also constitute (along with a "len" object) of a pointer (a unsafe.Pointer) to the underlying data. Hence, I was expecting the same behaviour as above but, apparently, I was wrong.
func f(s string) {
s = "bar"
}
func main() {
s := "foo"
f(s)
fmt.Println(s)
}
Output: "foo"
What is happening here with the string? Seems like the underlying data is being copied when the string is passed as argument.
Related question: When we do not wish our function to modify the string, is it still recommended to pass large strings by pointer for performance reasons?
A string has two values in it: pointer to an array, and the string length. When you pass string as an argument, those two values are copied, not the underlying array.
There is no way to modify the contents of string other than using unsafe. When you pass a *string to a function and that function modifies the string, the function simply modifies the string to point to a different array.
In the next code the first Println fails on build with error slice of unaddressable value. The rest of the lines are just fine.
package main
import "fmt"
func getSlice() [0]int {
return [...]int{}
}
func getString() string {
return "hola"
}
func main() {
fmt.Println(getSlice()[:]) // Error: slice of unaddressable value
var a = getSlice()
fmt.Println(a[:])
fmt.Println(getString()[:])
var b = getString()
fmt.Println(b[:])
}
Try this code
If the first Println is commented it works.
Try it out
Why is that? What I'm missing here?
What you're missing is that when slicing an array, the operand must be addressable ([0]int is an array, not a slice). And return values of function calls are not addressable. For details see How can I store reference to the result of an operation in Go?; and "cannot take the address of" and "cannot call pointer method on".
Spec: Slice expressions:
If the sliced operand is an array, it must be addressable and the result of the slice operation is a slice with the same element type as the array.
In this expression:
getSlice()[:]
getSlice() returns an array, and since it's the result of a function call, it's not addressable. Therefore you cannot slice it.
In this expression:
getString()[:]
getString() returns a string value, so it can be sliced even if the value is not addressable. This is allowed, because the result of the slice expression will be another string, and string values in Go are immutable.
Also, variables are addressable, so this will always work:
var a = getSlice()
fmt.Println(a[:])
getSlice() is not returning a slice it's returning an array, which is not addressable. You could return a pointer to the array:
func getSlice() *[0]int {
return &[...]int{}
}
or leave getSlice() as is and place the result in a temporary variable:
t := getSlice()
fmt.Println(t[:])
Is it possible using fmt.Sprintf() to replace all variables in the formatted string with the same value?
Something like:
val := "foo"
s := fmt.Sprintf("%v in %v is %v", val)
which would return
"foo in foo is foo"
It's possible, but the format string must be modified, you must use explicit argument indicies:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each formatting verb to format successive arguments passed in the call. However, the notation [n] immediately before the verb indicates that the nth one-indexed argument is to be formatted instead. The same notation before a '*' for a width or precision selects the argument index holding the value. After processing a bracketed expression [n], subsequent verbs will use arguments n+1, n+2, etc. unless otherwise directed.
Your example:
val := "foo"
s := fmt.Sprintf("%[1]v in %[1]v is %[1]v", val)
fmt.Println(s)
Output (try it on the Go Playground):
foo in foo is foo
Of course the above example can simply be written in one line:
fmt.Printf("%[1]v in %[1]v is %[1]v", "foo")
Also as a minor simplification, the first explicit argument index may be omitted as it defaults to 1:
fmt.Printf("%v in %[1]v is %[1]v", "foo")
You could also use text/template:
package main
import (
"strings"
"text/template"
)
func format(s string, v interface{}) string {
t, b := new(template.Template), new(strings.Builder)
template.Must(t.Parse(s)).Execute(b, v)
return b.String()
}
func main() {
val := "foo"
s := format("{{.}} in {{.}} is {{.}}", val)
println(s)
}
https://pkg.go.dev/text/template
I was using only advance function by passing two arguments. Can somebody help me to use it with three arguments which is illustrated as:
func advance<T : ForwardIndexType>(start: T, n: T.Distance, end: T) -> T
That function increments the start index by n positions, but not
beyond the end index.
Example: You want to truncate strings to a given maximal length:
func truncate(string : String, length : Int) -> String {
let index = advance(string.startIndex, length, string.endIndex)
return string.substringToIndex(index)
}
println(truncate("fooBar", 3)) // foo
println(truncate("fo", 3)) // fo
In the first call, the start index is incremented by 3 positions,
in the second example only by two. With
let index = advance(string.startIndex, length)
the second call would crash with a runtime exception, because
a string index must not be advanced beyond the end index.
I'm trying to write a mini DSL for some specific task. For this purpose I've been trying to solve a problem like this below (without using parantheses):
give me 5 like romanLetter
give me 5 like word
where the first line would return "V" and the second "five"
My definitions for the first part give me 5 look like this
def give = { clos -> clos() }
def me = { clos -> [:].withDefault { it
println it}
}
and then give me 5 prints 5
The problem is how to add more metaclass methods on the right. E.g.
give me 5 like romanLetter -> prints V OR
give me 5 like word -> prints five
my intuition is that I define like as
Object.metaClass.like = {orth -> if (orth.equals("roman")){ println "V"}
else {println "five"} }
this metaClass method like works only if there is a returned value from the left to be applied to, right? I tried adding a return statement in all of the closures which are on the left side but I always receive
groovy.lang.MissingPropertyException: No such property: like
for class: com.ontotext.paces.rules.FERulesScriptTest ...
do you have an idea how shall I do?
========================================
Here is the application of what I'm asking for.
I want to make a rule as follows
add FEATURE of X opts A,B,C named Y
where add is a closure, of, opts and named are MetaClass methods (at least that's how i imagine it), X, A, B, C, Y are parameters most probably strings and FEATURE is either a MetaClass property, or a closure without arguments or a closure with arguments.
If FEATURE does not take arguments then it is enough that add takes FEATURE as argument and returns a value on which
Object.metaClass.of will be executed with parameter X
Object.metaClass.opts will be executed on the returned by OF value with parameters A, B, C
Object.metaClass.named will be executed on the returned by opts value with parameter Y
each one of these metaclass methods sets its parameter as a value in a map, which is passed to a JAVA method when named is called.
I'm not sure this is the best solution for such a problem, but it seems to me such for the moment. The problem is if FEATURE is not a property itself but a closure which takes argument (e.g. feature1 ARG1). Then
add feature1 ARG1 of X opts A,B,C named Y
and this is the case which I'm stuck with. add feature1 ARG1 is the give me 5 part and I'm trying to add the rest to it.
========================================================
EXAMPLES:
I need to have both of the following working:
add contextFeature "text" of 1,2,3 opts "upperCase" named "TO_UPPER"
add length named "LENGTH"
where in the first case by parsing the rule, whenever each metaclass method of, opts, named is called I fill in the corresponding value in the following map:
params = [feature: "text",
of: 1,2,3,
opts: "upperCase",
named: "TO_UPPER"]
ones this map is filled in, which happens when named is parsed, I call a java method
setFeature(params.of, params.named, params.opts, params.feature)
In the second case length is predefined as length = "length", params values will be only
params = [feature : length,
of: null,
opts: null,
named: "LENGTH"]
and since of is null another java method will be called which is addSurfaceFeature(params.feature, params.named). The second case is more or less streight forward, but the first one is the one I can't manage.
Thanks in advance! Iv
You can do this sort of thing... Does that get you close?
def contextFeature( type ) {
"FEATURE_$type"
}
// Testing
new IvitaParser().parse {
a = add text of 1,2,3 opts "upperCase" named "TO_UPPER"
b = add length named "LENGTH"
c = add contextFeature( "text" ) of 1,2,3 opts "upperCase" named "TO_UPPER"
}
assert a == [feature:'text', of:[1, 2, 3], opts:'upperCase', named:'TO_UPPER']
assert b == [feature:'length', of:null, opts:null, named:'LENGTH']
assert c == [feature:'FEATURE_text', of:[1, 2, 3], opts:'upperCase', named:'TO_UPPER']
// Implementation
class IvitaParser {
Map result
def parse( Closure c ) {
c.delegate = this
c.resolveMethod = Closure.DELEGATE_FIRST
c()
}
def propertyMissing( String name ) {
name
}
def add( String param ) {
result = [ feature:param, of:null, opts:null, named:null ]
this
}
def of( Object... values ) {
result.of = values
this
}
def named( String name ) {
result.named = name
result
}
def opts( String opt ) {
result.opts = opt
this
}
}
You can even get rid of the quotes on the definition:
a = add text of 1,2,3 opts upperCase named TO_UPPER
b = add length named LENGTH
As the propertyMissing method just converts unknown properties into a String of their name