Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to use composite literal with maps but unable to use it as it shows some error.
please find the code below.
I am a newbie to Golang and perhaps have some less understanding about composite literals.
type Assessment struct{
StructuringForce map[string][]StructuringForce
}
type StructuringForce struct {
Principles map[string][]Capabilities
}
type Capability struct {
}
c1 := Capability{}
a1 := Assessment{
StructuringForce : map[string][]StructuringForce{
"Information Systems" , []StructuringForce{
StructuringForce{
Principles : map[string][]Capabilities{
"Integration of IT Services" ,[]Capabilities{
c1,
},
},
},
},
},
}
while constructing "a1" with composite literals I get "Missing key in map literals error".
But i can see that i have added keys.
As Volker pointed out, make cannot be used with literals. In your case it can either be:
make(map[string][]StructuringForce)
or
map[string][]StructuringForce{}{}
Secondly, for golang map, it's using : to separate the key-value, so it should be like:
a := map[string]string{
"foo": "bar",
}
Thirdly, you don't have Capabilities defined, so I suppose you're trying to do Capability.
To sum up, the entire thing in the main func should look like:
c1 := Capability{}
a1 := Assessment{
StructuringForce: map[string][]StructuringForce{
"Information Systems": []StructuringForce{
StructuringForce{
Principles: map[string][]Capability{
"Integration of IT Services": []Capabilities{
c1, // missing comma here as well
},
},
},
},
},
}
However, based on what you pasted, I would suggest you start from something straightforward to get started with the syntax and how to compose a map, like Go By Examples.
Another suggestion is that you can wrap the running code in main func when posting a SO question, which will make reproducing the issue a lot easier and more understandable to others who try to help.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type Testing struct {
firstname string
}
type Another struct {
*Testing
}
func main() {
var f = Another{firstname: "sasdf"}
fmt.Println(f)
}
Here I've used a pointer in the struct. Its something i've seen used in repository. But i'm not understanding.
What does this do? First i expected it would extend the properties of Testing struct. This ins't true.
From my inspected the Another struct may have a Testing property that holds a value. Giving it var f = Another{Testing: &Testing{firstname: "afsdf"}} and printing yields a struct containing a memory address. Do this syntax is a new struct with a property that contains a pointer to a object of T named the name of the type
From the spec:
A field declared with a type but no explicit field name is called an embedded field.
A field or method f of an embedded field in a struct x is called promoted if x.f is a legal selector that denotes that field or method f.
Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.
The last quoted sentence is why the composite literal Another{firstname: "sasdf"} did not work.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am intrigued with a question that an engineer at the company I work at asked me, about whether or not it is better to have a single function that traverses an array and tests two conditions or to have two functions, with a single condition each.
I came here to ask you guys if my rationale is wrong or not.
The code was something near this:
response := ListObjectsFromS3(bucket)
var filteredEmptyObjectsArray = utils.FilterEmptyObjects(response)
var filteredNonJson = utils.FilterNonJson(filteredEmptyObjectsArray)
With each function being:
func FilterEmptyObjects(arrayToFilter []*Object) []*Object {
var filteredArray []*Object
for _, object := range arrayToFilter {
if *object.Size > 0 {
filteredArray = append(filteredArray, object)
}
}
return filteredArray
}
func FilterNonJson(arrayToFilter []*Object) []*Object {
var filteredArray []*Object
for _, object := range arrayToFilter {
if strings.HasSuffix(*object.Key, ".json") {
filteredArray = append(filteredArray, object)
}
}
return filteredArray
}
Please pardon the repetition in the code above. It is meant as a toy example.
I don't know exactly how does Go optimizes this code, but I was thinking it might "squash" both functions into something like this - of course, not in Go code but the generated machine code would be equivalent to this:
func FilterSquashed(arrayToFilter []*Object) []*Object {
var filteredArray []*Object
for _, object := range arrayToFilter {
if strings.HasSuffix(*object.Key, ".json") && *object.Size > 0 {
filteredArray = append(filteredArray, object)
}
}
return filteredArray
}
And the code of the response - also not really in Go code, but the compiler would generate a machine code equivalent to something like this:
response := utils.FilterSquashed(ListObjectsFromS3(bucket))
The point is that when I made the objdump of the optimized code and the non-optimized one, both had the functions separated and would have a CALL to each function. So, I'm trying to understand what is the depth of optimization that is currently possible or that Go compiler decided to stick with.
Let me know your thoughts
The "squashed" code you show is not equivalent to the original code. The basic rule of code optimization is that the effects of the optimized and non-optimized code must be the same but in your example, you have two functions that apply different logic to filter a list, and a third function that would apply a third kind of logic that in this particular case would give you the composition of the two original functions, but not in the general case. So in short: no compiler would do what you are asking for in this case, because the semantics are different.
There may be cases when some functions are inlined the compiler might discover more optimizations, but I don't see how your example might benefit from inlining.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I did not get the below syntax in ../go/src/net/http/server.go:
var defaultServeMux ServeMux
where
ServeMux is a struct
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
es []muxEntry
hosts bool
}
In GO, type aliasing looks like type T1 = T2.
Is the above syntax(used for defaultServeMux) anything to do with type aliasing?
The line you quoted is just the declaration of a variable of type ServeMux which is the alternative, explicit variant of definition by assignment. For example these two statements are equivalent:
var foo ServeMux = ServeMux{}
foo := ServeMux{}
In both cases foo has the type ServeMux.
You can read about the variable declaration syntax here.
In the global variable scope the := shorthand is not allowed which is why the var syntax is used to define global variables (as is the case in your example).
Type "aliases", i.e. the declaration of new types based on existing types has a different syntax and has nothing to do with this.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I would like to declare a struct first and then initialize it inside a switch statement. The code I've written so far shows declared and not used errors. However, I think the problem is different in my case and related to scope of declaration.
Could somebody please help me to make the below code work?
Car.go
package main
import "fmt"
import "strconv"
type Car struct{
Name string
Price int
}
func main(){
name := "Fiat"
car := &Car{}
switch name {
case "Fiat":
car := &Car{
Name : "Fiat",
Price: 600000,
}
case "Mercedes-benz":
car := &Car{
Name : "Mercedes-benz",
Price: 5600000,
}
default:
car := &Car{
Name : "Toyota",
Price: 1000000,
}
}
fmt.Println("Car Name : " + car.Name + " Price : " + strconv.Itoa(car.Price));
}
Errors
$go run Car.go
./Car.go:19: car declared and not used
./Car.go:24: car declared and not used
./Car.go:29: car declared and not used
It's due to the scope of your variable declarations. You are shadowing the variable declaration inside the switch statement.
Simply change car:= to car= and you will be fine. You might also want to change car:=&Car{} to var car *Car. This will make your intent clearer and avoid an unnecessary allocation (as you are creating a new object which is never used).
Read about blocks & scopes and see the scoping section of the Go language reference.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am tying to download some data from parse (a string) and I get the error
"Value of type 'TableViewController' has no member 'place' on the line that says :
self.place.append(spotdetail.name!)
I have this as a global var :
var place = [""]
and this is my code in the ViewDidLoad:
let query = PFObject.query()
query!.findObjectsInBackgroundWithBlock ({ (objects, error) in
if let skateparks = objects {
for object in skateparks {
if let spotdetail = object as PFObject! {
self.place.append(spotdetail.name!)
self.tableView.reloadData()
}
}
}
print(place)
})
What can I change to make it work as I don't understand why it doesn't recognize the var place as it is in the same view controller (tableView)
thanks!
Everywhere in closure you should use self keyword for properties:
print(self.place)
As originaluser2 pointed out you are using a global variable so you do not need to user self.place. Also i'm not sure what you are subclassing in PFObject, but your func name is findObjectsInBackgroundWithBlock and you are reloading your table data there. Always keep in mind that you can only interact with the UI on the main thread. This will cause errors, so you can either pass back a callback, or do a GCD call to the main queue and then reload the data there.