I have a slice of struct. I am trying to copy this slice to new variable since my original slice changes a lot
model for sheet
type Timesheet struct {
ID *int64 `json:"id"`
TimestampStart *time.Time `json:"timestampStart"`
TimestampEnd *time.Time `json:"timestampEnd"`
}
SheetArrayCopy := make([]models.Sheet, len(sheetList))
copy(SheetArrayCopy, SheetList)
//several steps which goes through SheetList and changes the value of sheetList
However when I change the value in sheetList, the values of SheetArrayCopy also changes.
From your question and #Masklinn example link. I can see that you set value by using *pointer (address that pointer points to) which means set the value of that address to the new value.
There is nothing to do with
copy
Which is doing exactly which it means. And in this case, it clone the value of the fields' pointers which is pointing to the address of the fields' values.
The problem is the way you use and set value of the pointers.
There are 3 ways, to avoid the problem you mentioned about.
Write your custom clone slice which init new structs and clone only the values from original slice to the new.
Still using the clone, but when you set the value of the fields, set the fields' pointers to the new address. Others slices' items pointers still point to the old value.
Don't use pointer if you don't have any special reason.
You can ref to my code which is the demonstration of my answer.
https://play.golang.org/p/-pIgEDEr-hI
Link about the pointer which points out directly about how to use pointer.
https://tour.golang.org/moretypes/1
maybe you can covert that back to json and unmarsal that json to its new destination.
Related
Im new in Go-EVM, and some time ago I got a source code to get transaction tracking. But some functions in the source codes have been updated and changed, here are some questions I wanna ask:
The first one is: How to get *snapshot.Tree?
stateDB, err := state.New(block.Root(), state.NewDatabase(db))
Now this statements need three parameter and the lost parameter's type is *sanpshot.Tree. It is a struct, here is the link to its source code, in line 164.
The second one is: What are AsseccList and GasTipFee?
message := types.NewMessage(from, tx.To(), 0, tx.Value(), tx.Gas(), from Address, to Address, nonce, amount, gasLimit, tx.GasPrice(), GasTopfee, GasTipFee, tx.Data(), accesslist AccessList, false)
AccessList is also a struct. You can see its struct from here. What should I input into AccessList and GasTipFee?
Really appreciate it if you can help me solve these questions.
In your case you do not need to pass a tree snapshot if you do not have one. The purpose of the tree snapshot is to, if the snapshot matches with the given root of the block's trie, run what they call a pre-fetcher routine, that is in charge of preloading nodes in memory so that when the state reaches the commit phase, it is more performant because it already has most of the required nodes in memory. So in your case, you should be perfectly fine passing nil to that constructor.
As for the AccessList and GasTipFee parameters:
AccessList is an EIP-2930 access list. It's once again something optional that transactions can provide to specify the addresses and storage keys that they need access to. Once again you can provide a nil slice.
What you have called GasTipFee is called GasTipCap on master and is basically the limit value of a gas tip, as far as I understand. You can find more information on gas fees in the official documentation.
according to the manual,
The procedure on sysVar is called only when the value of the variable
changes. It can also be written as on sysVar_change. If you want to be
notified of value updates to the variable which don’t change the
value, you should use on sysVar_update instead.
In my example scenario, I have a system variable s::sysv of custom Struct Data Type X, where X has two fields: A and B.
In my CAPL script I put the following:
on sysvar_change s::sysv.A
{
// do stuff
}
Expected output is to do stuff only when s::sysv.A changes. However, since s::sysv.B is often updated when my simulation is running, then the procedure on sysvar_change s::sysv.A is called a lot more times than I expect, even if A doesn't change its value.
I don't understand why, and I'm putting a lot of workaround in place to avoid this, can anybody help?
Edit:
according to one reply, the event handler is not the struct element, but still the variable. However, the keyword this is now pointing to the struct element and not to the variable.
This bit of the manual is also relevant:
You can also react in the same way to value changes of specific
elements of a system variable of type struct or generic array. For
this, add the element to the name of the variable.
I have tried this functionality in the latest CANoe and it works as expected. The following is my code.
on key 'a'
{
#sysvar::Var_Struct1.StructMem1++;
}
on key 'b'
{
#sysvar::Var_Struct1.StructMem2++;
}
on sysvar_change Var_Struct1.StructMem1
{
write("StructMem1 value changed");
}
on sysvar_change Var_Struct1.StructMem2
{
write("StructMem2 value changed");
}
Whenever I press the key 'a' or 'b', the corresponding event is triggered.
Your variable is s::sysv. The event handler is called whenever the value of the variable changes. No matter whether A or B changes.
There is no way to restrict it only to certain changes of the value.
This is similar to the fact that you can also not be notified when, e.g. only the 3rd bit of an integer changes.
To me, it seems best to reconsider your setup and ask yourself, whether using the struct is the right approach, or whether it might be better to use two separate system variables A and B.
My goal is to get to the raw driver.Value values as deserialized by a sql driver in its implementation of driver.Rows.Next(). I want to handle the conversion from the values returned by the driver to the needed target types, instead of relying on the automatic conversions built in to Rows.Scan. Note this question does not ask your opinion on whether Rows.Scan "should" be used. I don't want to use it, and I am asking if there is any way to avoid it.
A meaningful answer does not use Rows.Scan at all. The dynamic approach illustrated in Working with Unknown Columns is awful: It invokes all the overhead of Scan and destroys the type information of the source columns, instead shredding the actual driver.Values into SqlBytes.
The following hack works, but relies on the internal implementation detail that sql.Rows.Next() populates the internal field lastcols with exactly the unconverted values which I want:
vpRows := reflect.ValueOf(rows) // rows is a *sql.Rows
vRows := reflect.Indirect(vpRows) // now we have the sql.Rows struct
mem := vRows.FieldByName("lastcols") // unexported field lastcols
unsafeLastCols := unsafe.Pointer(mem.UnsafeAddr()) // Evil
plastCols := (*[]driver.Value)(unsafeLastCols) // But effective
for rows.Next() {
rowVals := *plastCols
fmt.Println(rowVals)
}
The normal solution is to implement your own sql.Scanner. But this does use rows.Scan, so it violates your mysterious requirement not to use rows.Scan.
If you truly must avoid rows.Scan, you'll need to write your own driver implementation (possibly wrapping an existing driver) which provides access to the driver.Value values without rows.Scan.
I am new to Go, and trying to get the estimate size of a Thrift generated object in Go, the object has multiple level, and in the case of an member variable is an collection of pointers, I want to get the total size of pointer and the data that been pointed to. The sizeOf function won't work in this case, as it will only count the space taken by the pointer, not the actual space. What's the best way to get a good estimate of the size of the object? Ideally, I would like to breakdown the size into different fields and subfields
Here is how an example object looks like in Go
type Microshard struct {
Header *Header `thrift:"header,1,required"`
ValidatorStats []*ValidatorStat `thrift:"validatorStats,2,required"`
DataMap1 map[int64][]byte `thrift:"dataMap1,3,required"`
DataMap2 map[int64][]byte `thrift:"dataMap2,4,required"`
DataMap3 map[int64][]int64 `thrift:"dataMap3,5,required"`
DebugInfoMap map[string][]byte `thrift:"debugInfoMap,6,required"`
Index *IndexData `thrift:"index,7"`
}
Sorry to impose, but I would really appreciate it if someone would take a look at this and show me how to get this done:
Core Data works as expected with data associations between the Source list entry(s) and the upper right textField.
So does textField2 (lower right) if you manually type in a string.
I want to append a string in the lower textField, in this case a generic "Hello!" (implemented in the setText method) and have it also maintain it's association with the source list entry.
To summarize: textField2 - manually type in a string, it works as expected. Append the coded string, and it does not maintain it's association with the source list entry.
Here's the sample project.
Thanks again for the help.
Here's how I updated setText: method in MyDocument class:
-(IBAction)setText:(id)sender
{
NSString *newValue = [[output stringValue] stringByAppendingString:#"Hello!"];
[[setText selection] setValue:newValue forKey:#"textField2"];
}
I think your assumptions on value setting direction was wrong. Object does not take a value from the text field. It's the text field that takes value from the object. Hence I create newValue by taking current value of output text field and appending something to it. Then I take [setText selection] object (the one currently selected) and set it's textField2 property to new value. This setValue:forKey method automatically updates the output test field with new value of textField2 property.